Sitecore Power shell scripts to copy renderings from Shared to Final layout.
- May 10, 2023
- 2 min read
Updated: Dec 13, 2024
You may end up wanting to move renderings from shared to the final layout when in a multilingual set-up, you’d want to move a rendering to only apply to a specific language.
Reasons apart, though Sitecore Power Shell has provisions to move/merge renderings from Final to the Shared layout, there’s no out-of-the box function to do the vice versa.
To do this, you may use these scripts below. There are three scripts that you may need to run to achieve this -
A script to get the data sources of the rendering from the existing shared layout.
To remove the rendering from the shared layout
To add rendering to the Final layout of a Specific language version with the data sources extracted by the first script.
Below are the three scripts -
Sprint 1: To get the data sources of the rendering from the existing shared layout.
[IMPORTANT] — Once the script completes, copy the results from the result window (Host) as a CSV file.
$RootItem = “/sitecore/content/MyTenant/MySiteFolder/MySite/home/ParentPageFolder” $renderingID = “{A3CEF38E-231B-4369-BF22-BB9932542862}”
$deviceLayout = Get-LayoutDevice “Default”
$templateName = “myTemplate”
$placeHolder = “jss-main” #the place holder of the rendering
$items = Get-ChildItem -Path $RootItem -Recurse | Where-Object { $_.TemplateName -eq $templateName} -ErrorAction SilentlyContinue
$templateNameWrite-Host “Item,Type,Datasource”foreach($item in $items){$renderings = Get-Rendering -Item $item -Device $deviceLayout -Placeholder $placeHolder
foreach($rendering in $renderings){
if($rendering.ItemID -eq $renderingID){
if($rendering.DataSource){
$DSItem = Get-Item $rendering.DataSourceWrite-Host $item.Paths.FullPath,”,”,$item.TemplateName,”,”,$DSItem.Paths.FullPath }}}}
Script 2: To remove the rendering from the shared layout.
$RootItem = “/sitecore/content/MyTenant/MySiteFolder/MySite/home/ParentPageFolder” $renderingID = “{A3CEF38E-231B-4369-BF22-BB9932542862}”
$deviceLayout = Get-LayoutDevice “Default”
$templateName = “myTemplate”
$placeHolder = “jss-main”
$items = Get-ChildItem -Path $RootItem -Recurse | Where-Object { $_.TemplateName -eq $templateName} -ErrorAction SilentlyContinue
foreach($item in $items)
{
$renderings = Get-Rendering -Item $item -Placeholder $placeHolder
foreach($rendering in $renderings){
if($rendering.ItemID -eq $renderingID){
$item.Editing.BeginEdit();
Remove-Rendering -Item $item -Instance $rendering -Device $deviceLayout
Write-Host $rendering.ItemID “ removed from “ $item.Paths.FullPath$item.Editing.EndEdit();}}}
Script 3: To add rendering to the Final layout of a Specific language version with the data sources extracted from the first script.
$dataFolder = [Sitecore.Configuration.Settings]::DataFolder
$tempFolder = $dataFolder + “\temp\upload”
$filePath = Receive-File -Path $tempFolder -overwrite
$deviceLayout = Get-LayoutDevice “Default”
$renderingPath = “/sitecore/layout/Renderings/Project/ProjectFolder/RenderingFolder/Rendering”
$renderingItem = Get-Item -Database “master” -Path $renderingPath | New-Rendering -Placeholder “jss-main”
$Language = “en”
if($filePath -eq “cancel”){exit}$resultSet = Import-Csv $filePath$rowsCount = ( $resultSet | Measure-Object ).Count;if($rowsCount -le 0){Remove-Item $filePathexit}
foreach ( $row in $resultSet )
{
$item = Get-Item -Path $row.Item
if($item){
$item.Editing.BeginEdit();Add-Rendering -Item $item -Language $Language -PlaceHolder “jss-main” -Instance $renderingItem -Device $deviceLayout -Datasource $row.Datasource -FinalLayout
Write-Host “Added with “ $row.Datasource “ to “$item.Paths.FullPath$item.Editing.EndEdit(); }}
Remove-Item $filePath
Comments