Update Placeholder Settings using Sitecore Powershell Extensions

The following SPE script helps to update the Placeholder Settings of Sitecore items:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$rootItem = Get-Item -Path master:/sitecore/content/Cerner
$oldPlaceholderSettingItem = Get-Item -Path master:"/sitecore/layout/Placeholder Settings/Careers/careers-header"
$newPLaceholderSettingItem = Get-Item -Path master:"/sitecore/layout/Placeholder Settings/Careers/Structure/Header"
$reportOnly = $False
foreach($page in Get-ChildItem -Item $rootItem -Recurse){
    #Only items with a layout
    if (Get-Layout $page)
    {
        $oldRenderings = $page.__Renderings
        if($oldRenderings -like $oldPlaceholderSettingItem.Key){
            #Replace Path
            $newRenderings = $oldRenderings.Replace($oldPlaceholderSettingItem.FullPath, $newPLaceholderSettingItem.FullPath)
            #Replace Key
            $updatedRenderings = $newRenderings.Replace(($oldPlaceholderSettingItem.Name), $newPLaceholderSettingItem.Name)
            
            if (!$reportOnly)
            {
                $page.BeginEdit()
                $page.__Renderings = $updatedRenderings
                [void]$page.EndEdit()
            }
            Write-Host "Item: $($page.FullPath)"
        }
        
    }
}


This SPE script lets you specify the following parameters:

  • Root Item - The initial point where we are going to start updating items. 
  • New Placeholder Settings item path - The path of the new placeholder settings item. 
  • Old Placeholder Settings item path - The path of the old placeholder settings item.
  • Report Only - This is a flag that when set to true will only print the items that will be changed without actually updating the items.

Comments