I recently wrote my first Powershell script for removing an old rendering from a placeholder and replacing it with a new rendering. For some reason, when the rendering I need to remove is in the Final Layout, only, the script removes everything from the final layout, rather than just removing that one specific controller rendering. This is the script I'm using

$headerRendering = Get-Item -Path 'master:\sitecore\layout\Renderings\name\Metadata\Header\Header'

$targetDs = Get-Item -Path 'master:\sitecore\content\name\Site 
Assets\Header\DF Header'
$placeholder = 'layoutHeader'
$rendering = Get-Item -Path 'master:\sitecore\layout\Renderings\name\Metadata\Header\Header Builder' | New-Rendering -DataSource $targetDs -PlaceHolder $placeholder

$items = Get-ChildItem -Path 'master:\sitecore\content\name' -Recurse

foreach($item in $items) {
  $headerInstance = Get-Rendering -Item $item -Rendering $headerRendering -Placeholder $placeholder

  if($headerInstance) {
    Add-Rendering -Item $item -Rendering $rendering -Placeholder $placeholder
    Remove-Rendering -Item $item -Rendering $headerRendering -Placeholder $placeholder
    Write-Host "Updated $($item.Name)"

  $headerFinalInstance = Get-Rendering -Item $item -Rendering $headerRendering -Placeholder $placeholder -FinalLayout

  if($headerFinalInstance) {
    Remove-Rendering -Item $item -Rendering $headerRendering -Placeholder $placeholder -FinalLayout
    Add-Rendering -Item $item -Rendering $rendering -Placeholder $placeholder
    Write-Host "Removed Final Rendering $($item.Name)"

I'm not sure if I'm misunderstanding what the -FinalLayout flag does, or if I'm just misunderstanding the Remove-Rendering command. I believe I'm using SPE 4.5 for Sitecore 8.

  • The FinalLayout switch simply indicates to the final layout field id is used rather than the shared layout field id. Feb 20, 2018 at 18:19
  • It seems that the rendering you are adding is only on the shared layout, right? Feb 20, 2018 at 18:55
  • @HishaamNamooya yes I'm adding it to the shared layout. Feb 20, 2018 at 20:52
  • @MichaelWest, Is it not correct that if -FinalLayout is not specified, the rendering will be removed from Shared Layout? With that, the Final Layout would still contain the rendering, if it were specifically added to the Final Layout? If that's the case, then isn't it necessary to remove from both shared and Final? Feb 20, 2018 at 20:54


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.