Does anyone have a SPE script that could be used to upgrade the rendering parameters of existing content to the new dynamic placeholder style required for Sitecore 9 Dynamic placeholders?

We used to use: Placeholder Placeholder_1 Placeholder_2 etc...

Now required: Placeholder-{parent_rendering_guid}-0 Placeholder-{parent_rendering_guid}-1

The tricky part is getting that parent rendering guid to update the placeholder value with.

Thanks!

Here are two PowerShell scripts that we successfully ran to fulfill this task. The first creates a CSV file with all items that have dynamic placeholders. The second loops through the rows in the CSV and updates the dynamic placeholder rendering parameters.

Script 1:

  • Update the 'Home' node path and the 'basePageTemplateId' Guid

Write-Output "Start Script " (Get-Date).ToString('MM/dd/yyyy hh:mm:ss tt')

$elevatedUser= Get-User -Identity "sitecore\admin" New-UsingBlock (New-Object Sitecore.Security.Accounts.UserSwitcher $elevatedUser) {

    $database = "master" #master|web
    $startPath = "$($database):/sitecore/content/Home"
    $placeholderKey = "/main/*"

    $basePageTemplateId = [Sitecore.Data.ID]::Parse("{54BD9D21-90F2-4D22-A0BF-8747DD7C23B4}")

    $itemsWithPlaceholders = @()

    Get-ChildItem -Path $startPath -Language * -Recurse | ForEach-Object {
        $item = $_;
        $doesItInherit = [Sitecore.Data.Managers.TemplateManager]::GetTemplate($_.TemplateID, $_.Database).InheritsFrom($basePageTemplateId)

        #Write-Output "Outer Id: " $_.Id

        if($doesItInherit) {
            $renderings = Get-Rendering -Item $_  -Placeholder $placeholderKey -FinalLayout:$True
            foreach($rendering in $renderings){
                $matches = [regex]::Matches($rendering.Placeholder,'([0-9a-f]{8}[-][0-9a-f]{4}[-][0-9a-f]{4}[-][0-9a-f]{4}[-][0-9a-f]{12})$')
                if ($matches.Success) {

                    $itemWithPlaceholder = @{
                        Language = $_.Language
                        Version = $_.Version                        
                        ItemId = $_.Id
                    }
                    $itemsWithPlaceholders += New-Object PSObject -Property $itemWithPlaceholder

                    #Write-Output "Inner Id: " $_.Id
                    break
                }
            }
        }
    }

    $itemsWithPlaceholders | export-csv -Path C:\Temp\Items-With-Dynamic-Placeholders.csv -NoTypeInformation

    Write-Output "Done"
}

Script 2:

Write-Output "Start Script " (Get-Date).ToString('MM/dd/yyyy hh:mm:ss tt')

$csvPath = "C:\Temp\Items-With-Dynamic-Placeholders.csv"
$itemsWithDynamicPlaceholders = Import-Csv $csvPath

$placeholderKey = "/main/*"

foreach($row in $itemsWithDynamicPlaceholders) {

    Write-Host "Item details: " $row.ItemId ", " $row.Version ", " $row.Language

    $item = Get-Item -Path master: -ID $row.ItemId -Language $row.Language -Version $row.Version

    $renderings = Get-Rendering -Item $item -Placeholder $placeholderKey -FinalLayout:$True

    Write-Host $renderings.Count

    foreach($rendering in $renderings){

        $matches = [regex]::Matches($rendering.Placeholder,'([0-9a-f]{8}[-][0-9a-f]{4}[-][0-9a-f]{4}[-][0-9a-f]{4}[-][0-9a-f]{12})$')
        if ($matches.Success) {

            $renderingId = $matches.Groups[0].Value

            $newPlaceholder = $rendering.Placeholder.Replace($renderingId, "{$($renderingId.ToUpper())}-0")
            $newPlaceholder = $newPlaceholder.Replace("_", "-")
            $rendering.Placeholder = $newPlaceholder
            $Placeholder = $rendering.Placeholder
            try{

                #Write-Host "Update Attempt: "$item.Name ", " $renderingId ", " $Placeholder

                Set-Rendering -Item $item -Instance $rendering -FinalLayout:$True
            }
            catch{
                $counter = $counter + 1
                Write-Output (Get-Date).ToString('MM/dd/yyyy hh:mm:ss tt') " Error ItemId: " $item.ID.Guid " Name:  " $item.Name " Placeholder: " $Placeholder " Language: "  $item.Language.Name 
            }

        }
    }
}

Write-Output "Done"

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.