Friday, June 30, 2023

How to change placeholder key of rendering using PowerShell script in Sitecore?

Updating bulk content items manually might not be the best practice when dealing with bulk items.

This blog will help you update the placeholder key of renderings using the PowerShell script more efficiently. The following code snippet will be used to change the placeholder key of the rendering to the page programmatically using the PowerShell script.

This code should work with any version of Sitecore. 

Open - Development Tools -> PowerShell ISE and paste the following code.

Modify the paths and fields according to your template fields.


Update the placeholder key of the rendering on the existing page

PowerShell script code:    

$masterdatabase = "master"

$rootItem = Get-Item -Path (@{$true="$($masterdatabase):\sitecore\content\ArticleName\2023"; $false="$($masterdatabase):\content"}[(Test-Path -Path "$($masterdatabase):\sitecore\content\Home")])


$defaultLayout = Get-LayoutDevice "Default"

# Toggle for whether to update Shared or Final Layout

$useFinalLayout = $True

# If set to true, the script will only list the renderings that need fixing, rather than fixing them.

$reportOnly = $False

foreach ( $item in Get-ChildItem -Item $rootItem -Recurse )

{

    # Only interested in items that have a layout

    

    if (Get-Layout $item)

    {

      $itemTemplate = [Sitecore.Data.Managers.TemplateManager]::GetTemplate($item)

      if($itemTemplate.ID -eq "{HJUIUOOOL-4B42-4AD9-94F9-JKU78JJHH}")

      {

           

            $renderings =  Get-Rendering -Item $item -FinalLayout

            #Write-Host "$($item.FullPath) -($renderings)"

            foreach ( $rendering in $renderings )

            {

                # Only update the rendering if we're not in "Report Only" mode

                if (!$reportOnly)

                {

                    if($rendering.Placeholder -eq "sideBar")

                    {

                        $rendering.Placeholder = $rendering.Placeholder -replace "sideBar", "jss-article-sidebar"

                        Set-Rendering -Item $item -Instance $rendering -FinalLayout:$useFinalLayout

                        Write-Host "$($item.FullPath) - Rendering $($rendering.UniqueID) - Placeholder: $("sideBar") --> $("jss-article-sidebar")"

                    }

                    if($rendering.Placeholder -eq "main")

                    {

                        $rendering.Placeholder = $rendering.Placeholder -replace "main", "jss-main"

                        Set-Rendering -Item $item -Instance $rendering -FinalLayout:$useFinalLayout

                        Write-Host "$($item.FullPath) - Rendering $($rendering.UniqueID) - Placeholder: $("main") --> $("jss-main")"

                    }

                    if($rendering.Placeholder -eq "header")

                    {

                        $rendering.Placeholder = $rendering.Placeholder -replace "header", "jss-header"

                        Set-Rendering -Item $item -Instance $rendering -FinalLayout:$useFinalLayout

                        Write-Host "$($item.FullPath) - Rendering $($rendering.UniqueID) - Placeholder: $("header") --> $("jss-header")"

                    }

                    if($rendering.Placeholder -eq "footer")

                    {

                        $rendering.Placeholder = $rendering.Placeholder -replace "footer", "jss-footer"

                        Set-Rendering -Item $item -Instance $rendering -FinalLayout:$useFinalLayout

                        Write-Host "$($item.FullPath) - Rendering $($rendering.UniqueID) - Placeholder: $("footer") --> $("jss-footer")"

                    }

                }

            }

        }

    }

}



No comments:

Post a Comment

How to Create a Public Link Using the Sitecore Content Hub REST API

Creating a public link using the Sitecore Content Hub REST API is a straightforward process that enables you to share content externally whi...