Powershell For switch rendering

ok
03 Mar, 24
Sitecore

# Define the old and new rendering paths

$oldRenderingPath = "/sitecore/layout/Renderings/Old Rendering"

$newRenderingPath = "/sitecore/layout/Renderings/New Rendering"


# Get the old and new rendering items

$oldRenderingItem = Get-Item "master:$oldRenderingPath"

$newRenderingItem = Get-Item "master:$newRenderingPath"


# Check if both renderings exist

if ($oldRenderingItem -eq $null -or $newRenderingItem -eq $null) {

    Write-Host "One or both of the renderings do not exist."

    return

}


# Define the root path of the content tree to search

$rootPath = "/sitecore/content/"


# Fetch all items under the root path

$items = Get-Item -Path "master:$rootPath" -Recurse


foreach ($item in $items) {

    # Get all renderings associated with the current item

    $renderings = Get-Rendering -Item $item


    foreach ($rendering in $renderings) {

        if ($rendering.ItemID -eq $oldRenderingItem.ID) {

            # Switch rendering

            Set-Rendering -Item $item -Instance $rendering -Rendering $newRenderingItem

            

            # Optional: Write out the change for verification

            Write-Host "Switched rendering for item $($item.Paths.Path) from $($oldRenderingPath) to $($newRenderingPath)"

        }

    }

}