8

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!

1
  • Did you get anywhere with this by any chance?
    – Trayek
    Jun 27, 2019 at 9:06

1 Answer 1

2

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"
1
  • Reading through this code it seems this is for modules similar to the Fortis Dynamic Placeholders. In the placeholder it has the GUID so it's a matter of reformatting it. The Integrated Dynamic Placeholder module has /main/placeholder_SCEXP1 for instance - so the mapping of placeholder to parent rendering GUID has to be created
    – Trayek
    Jun 27, 2019 at 9:10

Your Answer

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

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