Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 8, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 8, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 8, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 8, 2025

      Xbox handheld leaks in new “Project Kennan” photos from the FCC — plus an ASUS ROG Ally 2 prototype with early specs

      May 8, 2025

      OpenAI plays into Elon Musk’s hands, ditching for-profit plan — but Sam Altman doesn’t have Microsoft’s blessing yet

      May 8, 2025

      “Are we all doomed?” — Fiverr CEO Micha Kaufman warns that AI is coming for all of our jobs, just as Bill Gates predicted

      May 8, 2025

      I went hands-on with dozens of indie games at Gamescom Latam last week — You need to wishlist these 7 titles right now

      May 8, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Mastering Node.js Streams: The Ultimate Guide to Memory-Efficient File Processing

      May 8, 2025
      Recent

      Mastering Node.js Streams: The Ultimate Guide to Memory-Efficient File Processing

      May 8, 2025

      Sitecore PowerShell commands – XM Cloud Content Migration

      May 8, 2025

      Our Partner Adobe Recognized Again as a DXP Leader

      May 8, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Xbox handheld leaks in new “Project Kennan” photos from the FCC — plus an ASUS ROG Ally 2 prototype with early specs

      May 8, 2025
      Recent

      Xbox handheld leaks in new “Project Kennan” photos from the FCC — plus an ASUS ROG Ally 2 prototype with early specs

      May 8, 2025

      OpenAI plays into Elon Musk’s hands, ditching for-profit plan — but Sam Altman doesn’t have Microsoft’s blessing yet

      May 8, 2025

      “Are we all doomed?” — Fiverr CEO Micha Kaufman warns that AI is coming for all of our jobs, just as Bill Gates predicted

      May 8, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Sitecore PowerShell commands – XM Cloud Content Migration

    Sitecore PowerShell commands – XM Cloud Content Migration

    May 8, 2025

    In this post, I’ve listed the most commonly used Sitecore PowerShell commands for content migration. This blog continues from my earlier post: Sitecore XM Cloud Content Migration: Plan and Strategy.

    During migration, we created several PowerShell scripts to extract data from the legacy database to CSVs. We then used those CSVs to import content into XM Cloud instances. Based on those scripts, I organized the commands into two groups: Working with Sitecore items and Working with Sitecore renderings. These commands aim to help developers handle similar Sitecore to XM Cloud migrations.

    Working With Sitecore Items

    Create a New Item Using the Template ID

    $item = New-Item -Path $path -Name $itemName -ItemType $itemTemplateId -Language "en"

    Sometimes, you may need to create an item with the same ID as in the legacy system to avoid numerous reconfigurations. Especially if those items are being used as the data source. In such a use case, we could use CreateItem from Sitecore.Data.Managers.ItemManager. This method takes the item name, parent item, template ID, and item ID. The passed $id will be the ID of the newly created Sitecore Item. 

    $item = [Sitecore.Data.Managers.ItemManager]::CreateItem($name, $parentItem, $templateItem.ID, $id)

    Also, there is a ForceId param supported by the ‘New Item‘ function

    New-Item -Path $path -Name $name -ItemType "Blog Page" -ForceId "3904b0bf-b10b-4fbb-9ced-3de87dfa3d48"

    Create a New Item Using the Branch Template

    $item = [Sitecore.Data.Managers.ItemManager]::AddFromTemplate($itemName, $branchTemplateId, $parentItem)

    Checking if the Path Exists in the Content Tree

    In use cases, we need to check whether the path exists before creating an item on that path. 

    $pathExists = Test-Path -Path $path
    if($pathExists)
    {
      //logic
    }

    Copying Items

    Copy-Item -Path $sourcePath -Destination $targetPath

    Working with Sitecore Renderings

    Get All Renderings for an Item

    This script was used to analyze an item’s legacy renderings, map them with new XM cloud renderings (components), and map fields. 

    $item = Get-Item -Path $path -Version "latest"
    $resultObj = @()
    $defaultLayout = Get-LayoutDevice "Default"
    Get-Rendering -Item $item -Device $defaultLayout -FinalLayout | ForEach {
        $renderingItem = Get-Item -Path master: -ID $_.ItemID
        $Obj = @{
            RenderingName =  $renderingItem.Name
            RenderingId =  $_.ItemID
            DataSource = $_.Datasource
            Placeholder = $_.Placeholder
            PageItem = $_.OwnerItemID
    }
        $resultObj += New-Object psobject -Property $Obj
    }
    $resultObj | Format-Table RenderingName, RenderingId, DataSource, Placeholder, PageItem

    Create and Set Rendering for an Item

    When importing data from CSV, we often need to create and set a data source to render an item.  For this use case, I created a function that takes the rendering ID, the placeholder to add the rendering, and the data source ID. 

    function CreateAndSetRendering{
        param([String]$id,[String]$placeholder,[String]$dsid
            )
            
            $renderingId = [Sitecore.Data.ID]::Parse($id)
            $rendering = get-item -path master: -id $renderingId
            $renderinginstance = $rendering | new-rendering -placeholder $placeholder
            if($dsid -ne "")
            {
                $datasourceId = [Sitecore.Data.ID]::Parse($dsid)
                $renderinginstance.datasource = $datasourceId
            }
            add-rendering -item $item -placeholder $placeholder -instance $renderinginstance -finallayout
            $item.editing.beginedit()
            $item.editing.endedit() | out-null
    }

    Retrieve the Rendering and Remove From the Presentation

    {3904b0bf-b10b-4fbb-9ced-3de87dfa3d48} is the Sitecore Item ID of the rendering item we wish to retrieve

    $defaultLayout = Get-LayoutDevice "Default"
    $rendering = Get-Rendering -Item $item -Device $defaultLayout -FinalLayout | Where-Object { $_.ItemID -eq "{3904b0bf-b10b-4fbb-9ced-3de87dfa3d48}"}
    Remove-Rendering -Item $item -Instance $rendering -Device $defaultLayout -FinalLayout

    Getting a Specific Rendering Parameter Value

    $paraName is the rendering parameter name, for example, “Styles”.

    $rendering = Get-Item -Path master: -Id "{3904b0bf-b10b-4fbb-9ced-3de87dfa3d48}"
    $renderingItem = Get-Rendering -Item $item -Device $defaultLayout -Rendering $rendering -FinalLayout
    $parameterValue = Get-RenderingParameter -Rendering $renderingItem -Name $paramName

    Updating Rendering Parameter Value

    If there are more than one rendering of the same type, the returned $renderingItem will be an array so you can access the first rendering parameters $renderingItem[0].Parameters: This will return all parameters, and then you will have to check for a specific parameter.

    $rendering = Get-Item -Path master: -Id "{3904b0bf-b10b-4fbb-9ced-3de87dfa3d48}"
    $renderingItem = Get-Rendering -Item $item -Device $defaultLayout -Rendering $rendering -FinalLayout
    $renedringParams = $renderingItem[0].Parameters
    $styles = "Styles"
     if ($renedringParams.Contains($styles)) {
          $renedringParams = @{
                Styles = "%7B3904b0bf-b10b-4fbb-9ced-3de87dfa3d48%7D"
        }
        }
    Set-RenderingParameter -Instance $renderingItem[0] -Parameter $renedringParams | Out-Null
    Set-Rendering -Item $item -Instance $renderingItem[0] -FinalLayout

    Note: We must embed Sitecore ID for your required style between %7B and %7D. For multiple values, the separator is %7D%7C%7B. It’s how Sitecore stores params values.

    You can store multiple values like this: Styles = “%7B3904b0bf-b10b-4fbb-9ced-3de87dfa3d48%7D%7C%7B936219ee-a03b-49c5-8eff-8b877b5c1319%7D”

    Conclusion

    So, this is the consolidated list of Sitecore PowerShell commands for content migration. The IDs used in the above snippets were not valid Sitecore item IDs. Replace them with valid Sitecore item IDs based on the Sitecore items used in your project.

    Keep learning!

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleOur Partner Adobe Recognized Again as a DXP Leader
    Next Article Mastering Node.js Streams: The Ultimate Guide to Memory-Efficient File Processing

    Related Posts

    Development

    February 2025 Baseline monthly digest

    May 8, 2025
    Development

    The Front-End Performance Optimization Handbook – Tips and Strategies for Devs

    May 8, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Why this versatile air pump is my new must-have for traveling (and it’s only $42)

    Development

    为 AI 重新定义数据库:MongoDB 为何收购 Voyage AI

    Databases

    CVE-2025-24522 – KUNBUS Revolution Pi Node-RED Remote Command Execution

    Common Vulnerabilities and Exposures (CVEs)

    Worlds Collide: Keyframe Collision Detection Using Style Queries

    News & Updates

    Highlights

    Development

    Composable Martech: Experience Builders

    April 18, 2024

    Welcome back for Part 2 in a series on composable martech, where we unpack the…

    Meet LLaVA-o1: The First Visual Language Model Capable of Spontaneous, Systematic Reasoning Similar to GPT-o1

    November 18, 2024

    8 Tips for Kubernetes Role-Based Access Control (RBAC)

    April 26, 2025

    Android 15 Beta 3 introduces improvements to passkey user experience

    June 19, 2024
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.