Modernizing Sitecore Pages: Script-Based Removal of Default Components After Page Design Adoption

Hello everyone, In this blog I will share one most important powershell script that I personally extensively used while working on XMC migration project.

Our migration project was from XP to XM Cloud which had whole UI redesign but Sitecore CMS structure remain same. Intially, while migration we followed the traditional Sitecore approach to add the renderings to standard value of the template as they were added in legacy. But when most of the component was developed, we though of utilizing the Page Design and Partial Design feature of SXA.

The most commong thing which can go to page design and partial design was header, footer, breadcrumb etc which was common for all pages.

Now the major problem here was, in legacy, Header and Footer for example was added manually to page level items. So we faced duplication issue as the components are coming from page design as well as standard values. To overcome this issue, the best solution was to remove the components from standard values of templates which are now coming from page design.

So, to do this changes on multiple items, the best was and always our savior was Sitecore PowerShell. Below is the script which was used to remove the renderings from standard values as well as page items based on placeholder keys.

Example: Remove all renderings where the placeholder (ph attribute) contains either "headless-header" or "headless-footer"

       
 $item = Get-Item -Path "master:/sitecore/templates" -Recurse
$standardValueId = $item.Fields["__Standard values"].Value

if ($standardValueId) {
    $stdItem = Get-Item -Path master: -Id $standardValueId -ErrorAction SilentlyContinue
    if ($stdItem -ne $null) {
        $xml = $stdItem.Fields["__Renderings"].Value
        if ($xml) {
            [xml]$layout = $xml

            #Define placeholders to remove (regex match)
            $placeholdersToRemove = @("headless-header", "headless-footer")

            #Find all renderings to remove
            $renderingsToRemove = @()
            foreach ($r in $layout.r.d.r) {
                foreach ($ph in $placeholdersToRemove) {
                    if ($r.ph -match $ph) {
                        Write-Host "Removing rendering UID: $($r.uid), Placeholder: $($r.ph), ID: $($r.id)"
                        $renderingsToRemove += $r
                        break
                    }
                }
            }

            #Remove those renderings
            foreach ($r in $renderingsToRemove) {
                [void]$r.ParentNode.RemoveChild($r)
            }

            #Save updates
            $stdItem.Editing.BeginEdit()
            $stdItem.Fields["__Renderings"].Value = $layout.OuterXml
            $stdItem.Editing.EndEdit()

            Write-Host "Removed $($renderingsToRemove.Count) rendering(s) from 'headless-header' and 'headless-footer' placeholders."
        } else {
            Write-Host "No __Renderings value found on standard values item."
        }
    } else {
        Write-Host "Standard values item not found."
    }
} else {
    Write-Host "No standard values ID found on template."
}
      
	   

You can utilize this logic according to your requirement by modifying the script. Thank you for reading this blog and keep learning.

You can check my other blogs too if interested. Blog Website

Comments

Popular posts from this blog

Automate RSS Feed to Sitecore XM Cloud: Logic App, Next.js API & Authoring API Integration

Sitecore XM Cloud Form Integration with Azure Function as Webhook

Sitecore 10.2 Installation using Windows PowerShell