# IDs of the old and new renderings
$oldRenderingId = "{OLD-RENDERING-ID}"
$newRenderingId = "{NEW-RENDERING-ID}"
# Fetch all items in the master database
$items = Get-ChildItem -Path "master:\content" -Recurse
foreach ($item in $items) {
# Iterate through both shared and final layouts
foreach ($layoutField in @("__Renderings", "__Final Renderings")) {
$layoutXml = [Sitecore.Layouts.LayoutDefinition]::Parse($item[$layoutField])
# Check if the layout XML contains the old rendering
$deviceLayout = $layoutXml.Devices | Where-Object { $_.Renderings.Renderings | Where-Object { $_.ItemID -eq $oldRenderingId } }
if ($deviceLayout) {
# Replace the old rendering ID with the new rendering ID
foreach ($rendering in $deviceLayout.Renderings.Renderings) {
if ($rendering.ItemID -eq $oldRenderingId) {
$rendering.ItemID = $newRenderingId
}
}
# Begin editing the item
$item.Editing.BeginEdit()
try {
# Update the layout field with the new XML
$item[$layoutField] = $layoutXml.ToXml()
$item.Editing.EndEdit()
}
catch {
$item.Editing.CancelEdit()
}
}
}
}
# Define the IDs of the old and new renderings
$oldRenderingId = "{OLD-RENDERING-ID}"
$newRenderingId = "{NEW-RENDERING-ID}"
# Define the path to start searching from, e.g., "/sitecore/content/Home"
$startPath = "master:/sitecore/content"
# Retrieve all items under the specified path
$items = Get-ChildItem -Path $startPath -Recurse
foreach ($item in $items) {
# Process both shared and final layouts
foreach ($fieldId in @("{F1A1FE9E-A60C-4DDB-A3A0-BB5B29FE732E}", "{04BF00DB-F5FB-41F7-8AB7-22408372A981}")) { # Field IDs for Shared and Final Layouts
$layoutField = $item.Fields[$fieldId]
if ($layoutField -and $layoutField.Value) {
$layoutXml = [Sitecore.Layouts.LayoutDefinition]::Parse($layoutField.Value)
$modified = $false
foreach ($device in $layoutXml.Devices) {
# Iterate through all renderings in the device
foreach ($rendering in $device.Renderings.Renderings) {
if ($rendering.ItemID -eq $oldRenderingId) {
# Capture datasource, parameters, and placeholder before updating
$dataSource = $rendering.Datasource
$parameters = $rendering.Parameters
$placeholder = $rendering.Placeholder
# Update the rendering details
$rendering.ItemID = $newRenderingId
$rendering.Datasource = $dataSource
$rendering.Parameters = $parameters
$rendering.Placeholder = $placeholder
$modified = $true
}
}
}
if ($modified) {
# Begin editing the item
$item.Editing.BeginEdit()
try {
# Update the layout field with the modified layout XML
$layoutField.Value = $layoutXml.ToXml()
$item.Editing.EndEdit()
Write-Host "Updated rendering for item: $($item.Paths.Path)"
}
catch {
$item.Editing.CancelEdit()
Write-Host "Failed to update rendering for item: $($item.Paths.Path)"
}
}
}
}
}