Effortless Content Migration: Using PowerShell to Update Placeholders for XMC
Hello everyone, In this blog I will share one most important powershell script that I personally extensively used while woring on XMC migration project.
As you might be aware that there are many types of layout services which can bes used to fetch your layout in XMC but XMC by default used sxa-jss layout
Sitecore has also provided information around how we should implement dynamic placeholders in their KB page in proper way but most of the time when we work in migration project in headless, we used component's uid to create dynamic placeholders.
So here's the question arise, if we want to follow what Sitecore recommends then how we can change all the placeholders automatically. To overcome this problem I have created a powershell script which will do the magic for us. Below is the script that you can used or modify according to your needs.
$item = Get-Item -Path "master:/sitecore/templates" -Recurse
$standardValueId = $item.Fields["__Standard values"].Value
if ($standardValueId) {
$stdItem = Get-Item -Path master: -Id $standardValueId -ErrorAction SilentlyContinue
if ($stdItem -ne $null) {
$xml = $stdItem.Fields["__Renderings"].Value
if ($xml) {
[xml]$layout = $xml
# 1. Build UID → ComponentName lookup
$uidMap = @{}
$layout.r.d.r | ForEach-Object {
$renderingItem = Get-Item -Path "master:" -Id $_.id
# Default to null
$dynamicId = $null
if ($_.par) {
# Split parameters by '&'
$params = ($_.par -split "&") | ForEach-Object { $_ -replace "amp;","" }
foreach ($p in $params) {
Write-Output "p -> $p"
if ($p -match "^DynamicPlaceholderId=(\d+)$") {
$dynamicId = $matches[1]
Write-Output "Dynamic ID -> $dynamicId"
}
}
}
$uidMap[$_.uid.ToUpper()] = @{
ComponentName = $renderingItem.Name
DynamicPlaceholderId = $dynamicId
}
}
# 2. Update placeholders
$layout.r.d.r | ForEach-Object {
$oldPh = $_.ph
if ($oldPh -and $oldPh -match '\{[0-9a-fA-F\-]+\}-\d+') {
if ($oldPh -match '\{([0-9a-fA-F\-]+)\}-\d+') {
$uidOnly = $matches[1].ToUpper()
$fullUid = "{$uidOnly}"
Write-Output "FullUid -> $fullUid"
if ($uidMap.ContainsKey($fullUid)) {
$dynamicId = $uidMap[$fullUid].DynamicPlaceholderId
$parentPh = $oldPh -replace '-\{[0-9a-fA-F\-]+\}-\d+$',''
Write-Output "parent ph -> $parentPh"
$newPh = "$parentPh-$dynamicId"
$_.ph = $newPh
Write-Output "Updated PH: $oldPh -> $newPh"
}
}
}
}
# 3. Save updates
$stdItem.Editing.BeginEdit()
$stdItem.Fields["__Renderings"].Value = $layout.OuterXml
$stdItem.Editing.EndEdit()
}
}
}
This script will gives an example of one single item standard value modification but you can literally fetch all child items and perform it one go.
Output:
Thank you for reading and keep learning!!
You can check my other blogs too if interested. Blog Website



Comments
Post a Comment