0

I need to transfer not full layout only some common renderings from the Final layout to the Shared layout in the presentation details section for over 100 pages in Sitecore 10.2.

I've attempted to write a PowerShell script for this task, but it's not working correctly. Could someone provide guidance on the proper approach or help troubleshoot my script?

param(
    [string]$itemPath = "/sitecore/content/Home",        # Path to the item
    [string]$renderingName = "CommonRendering"           # Name of the rendering to move
)

# Get the target item
$item = Get-Item -Path $itemPath
if (-not $item) {
    Write-Host "Item not found at path: $itemPath"
    return
}

# Access the default layout device
$defaultLayout = Get-LayoutDevice -Default       

# Get renderings from the Final layout
$finalRenderings = Get-Rendering -Item $item -Device $defaultLayout -FinalLayout

# Find the rendering to move
$renderingToMove = $finalRenderings | Where-Object {
    $_.RenderingItem.Name -like "*$renderingName*"
}

if (-not $renderingToMove) {
    Write-Host "Rendering '$renderingName' not found in Final Layout."
    return
}

# Remove rendering from Final Layout
Remove-Rendering -Item $item -Rendering $renderingToMove -Device $defaultLayout -FinalLayout

# Add rendering to Shared Layout
Add-Rendering -Item $item -Rendering $renderingToMove -Device $defaultLayout -SharedLayout

Write-Host "Rendering '$renderingName' successfully moved from Final to Shared layout."

I have tried with this question but this is not working in my case.

Copy final layout to shared layout

In my case I need to transfer not full layout only some common renderings from the Final layout to the Shared layout.

5
  • Try the answer here: sitecore.stackexchange.com/a/4623/237 Commented Apr 23 at 11:51
  • Gatogordo - I have tried with this question but this is not working in my case. Commented Apr 23 at 12:35
  • Can you tell us why it is not working in your particular case? Commented Apr 23 at 12:50
  • I have used complete function and executed in SPE but nothing happed in background. can you please try at your end. Commented Apr 23 at 12:52
  • I need to transfer not full layout only some common renderings from the Final layout to the Shared layout Commented Apr 23 at 13:32

2 Answers 2

3

To copy some common renderings from final layout to shared layout, run this Sitecore PowerShell code snippet.

function Copy-RenderingToSharedLayout {
    param(
        [Parameter(Mandatory = $true)]
        [Alias("itemPath")]
        [string[]]$itemPaths,  # Accepts single or multiple paths

        [Parameter(Mandatory = $true)]
        [Alias("renderingId")]
        [string[]]$renderingIds  # Accepts single or multiple rendering IDs
    )

    $defaultLayout = Get-LayoutDevice -Default       

    foreach ($itemPath in $itemPaths) {
        $item = Get-Item -Path $itemPath
        if (-not $item) {
            Write-Host "Item not found at path: $itemPath"
            continue
        }

        $finalRenderings = Get-Rendering -Item $item -Device $defaultLayout -FinalLayout
  $sharedRenderings = Get-Rendering -Item $item -Device $defaultLayout

        foreach ($renderingId in $renderingIds) {
            $renderingToMove = $finalRenderings | Where-Object { $_.ItemId -eq $renderingId }

            if (-not $renderingToMove) {
                Write-Host "Rendering with ID '$renderingId' not found in Final Layout for item $itemPath."
                continue
            }

            $renderingToMove | ForEach-Object {
                $parameters = Convert-StringToHashTable -parameters $_.Parameters
                Add-Rendering -Item $item -PlaceHolder $_.Placeholder -Instance $_ -Parameter $parameters
            }

            $renderingToBeRemoved = Get-Item -Path "master:" -ID $renderingId
            Remove-Rendering -Item $item -Rendering $renderingToBeRemoved -Device $defaultLayout -FinalLayout

            Write-Host "Moved rendering $renderingId from final to shared layout on item $itemPath"
        }
  
     # Again Add renderig to fianl layout
      foreach ($renderingId in $renderingIds) {
            $renderingToMove = $sharedRenderings | Where-Object { $_.ItemId - 
                eq $renderingId }

            if (-not $renderingToMove) {
                Write-Host "Rendering with ID '$renderingId' not found in 
               Final Layout for item $itemPath."
                continue
            }

            $renderingToMove | ForEach-Object {
                $parameters = Convert-StringToHashTable -parameters $_.Parameters
                Add-Rendering -Item $item -PlaceHolder $_.Placeholder -Instance $_ -Parameter $parameters -FinalLayout
            }

        }
    }
}


Copy-RenderingToSharedLayout -itemPaths @(
    "/sitecore/content/Home/MyProject/Home/blog",
    "/sitecore/content/Home/MyProject/Home/news"
) -renderingIds @(
    "{669223AE-10E7-468F-B538-C3CDF44A672A}",
    "{A22D385D-3095-4218-B5C6-F76B3D816A9E}"
)

This script lets you work with many renderings and pages at once. You just need to add itemPaths and renderingIds .

Hope this help!!

0

In order to copy the rendering from final layout to shared layout, you can run the below sitecore powershell code snippet.

function Convert-StringToHashTable{
    param(
    [string]$parameters
    )   
    $hashtable = @{}
    if($parameters -ne $null){
        $parameters.Split('&') | ForEach-Object {
            $pair = $_.Split('=')
            if($pair[1] -ne $null){
                $hashtable.Add($pair[0],[uri]::UnescapeDataString($pair[1]))
            }
        }
    }
    [hashtable]$hashtable
}

function Copy-RenderingToSharedLayout {
param(
    [string]$itemPath = "/sitecore/content/Home",        # Path to the item
    [string]$renderingId = "ID of the rendering to remove"           # Name of the rendering to move
)

    # Get the target item
    $item = Get-Item -Path $itemPath
    if (-not $item) {
        Write-Host "Item not found at path: $itemPath"
        return
    }

    # Access the default layout device
    $defaultLayout = Get-LayoutDevice -Default       

    # Get renderings from the Final layout
    $finalRenderings = Get-Rendering -Item $item -Device $defaultLayout -FinalLayout

    # Find the rendering to move

    $renderingToMove = $finalRenderings | Where-Object {
        $_.ItemId -eq $renderingId
    }

    if (-not $renderingToMove) {
        Write-Host "Rendering '$renderingName' not found in Final Layout."
        return
    }

    #add rendering to the shared layout
    $renderingToMove | ForEach-Object {
        $parameters = Convert-StringToHashTable -parameters $_.Parameters
        Add-Rendering -Item $item -PlaceHolder $_.Placeholder -Instance $_ -Parameter $parameters
    }
    
    #remove rendering from the final layout
    $renderingToBeRemoved = Get-Item -Path "master:" -ID $renderingId
    Remove-Rendering -Item $item -Rendering $renderingToBeRemoved -Device $defaultLayout -FinalLayout
}


Copy-RenderingToSharedLayout -itemPath "/sitecore/content/TestSiteCollection/TestSite/Home" -renderingId "{3972C278-7E7A-4C2B-A293-66AF860F5C3A}"

Hope this helps. Let me know in case you face any issues.

3
  • You have tested at your end first? Commented Apr 23 at 10:52
  • yes @Kiran I have updated the code snippet above. Let me know in case you face any issues. Commented Apr 23 at 18:46
  • This is working but duplicating in final layout. Commented May 9 at 4:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.