Powershell script to change the layout of the site pages

To change the layout of all site pages in Sitecore using PowerShell, you can utilize the Sitecore PowerShell Extensions (SPE) module. Here’s how you can achieve this:

Below is the the powershell script where that sets the desired layout for some particular site pages.

# Specify the path to your site's root item
$path = "/sitecore/content/Site/Home"

#Specify the page item language version
$language = "en"

#Content page items which you want to consider for layout change
$contentTemplateIDs = "{0F92C6BD-2E83-4AD3-BCF4-8AD8EC0DAC3C}{88BE71CD-8861-47D6-B32F-6CDA8388D14B}{87C615A9-C647-4CA3-92BB-A6655CEAFE53}"

#Global Layout item path
$Globallayout = Get-Item -Path '/sitecore/layout/Layouts/Common/Global Layout'

# Filters all the pages that require changes
$pages = Get-ChildItem -Path "master:$path" -Language $language -Recurse -WithParent | Where-Object { $contentTemplateIDs.Contains($_.TemplateID.ToString()) }

# Get the device for which you want to set the layout (e.g., 'Default')
$device = Get-LayoutDevice -Default

foreach ($page in $pages) {
if($page.Name -ne "XYZ page")
{
#Sets the layout in the final layout in presentation details.
Set-Layout -Item $page -Device $device -Layout $Globallayout -FinalLayout | Out-Null
Write-Host $page.Name
#Displays the layout on the page
Get-Layout $page.FullPath -FinalLayout
}
}


You can download this file from my GIST

Here you can localise the values as required like siteroot path, language, layoutpath and page name or you can remove that conditions, also the update the content template IDs.

Now, you can execute the script in the Sitecore PowerShell Console. It will traverse the site pages and update their layouts.

Remember to adjust the script according to your specific site structure and layout requirements.

Happy scripting!!!

Comments