Skip to main content

swap renderings

This PS script will, based on the paths and rendering id's provided, swap renderings and publish the affected items.

$pathsToChange = @(
"master:/sitecore/path/to/page/1",
"master:/sitecore/path/to/page/2",
)

$defaultLayout = Get-LayoutDevice "Default"

$sourceRenderingId = "{00000000-0000-0000-0000-000000000000}"
$targetRenderingId = "{00000000-0000-0000-0000-000000000000}"

foreach($path in $pathsToChange)
{
$isUpdated = $false
$item = Get-Item -Path $path

$sourceRenderings = Get-Rendering -Item $item -Device $defaultLayout -FinalLayout |
Where-Object { $_.ItemID -eq $sourceRenderingId }

$targetRendering = Get-Item -Path "master:" -ID $targetRenderingId | New-Rendering

# Replace all instances of the old rendering with the new rendering
foreach($sourceRendering in $sourceRenderings) {
Write-Output "Switching renderings...."
$item | Switch-Rendering -Instance $sourceRendering -Device $defaultLayout -FinalLayout -NewRendering $targetRendering
$isUpdated = $true
}

if($isUpdated)
{
Publish-Item -Id $item.ID -Recurse -PublishMode Full -Language '*' -RepublishAll -Verbose
}
}