Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Adamsimsy/fc79bb9202d9bc7f1031862a3a551ae7 to your computer and use it in GitHub Desktop.
Save Adamsimsy/fc79bb9202d9bc7f1031862a3a551ae7 to your computer and use it in GitHub Desktop.
Sitecore fix dynamic placeholder keys
$startPath = "/sitecore/content/home"
$placeholderKey = "*_*"
$outputFilePath = "C:\temp\placeholder-replacement-report.csv"
$results = @();
Write-Progress -Activity "Upgrading dynamic placeholders that for the key [$($placeholderKey)]" `
-Status "Getting content items"
Get-ChildItem -Path $startPath -Recurse -Version * | ForEach-Object {
$item = $_;
Write-Progress -Activity "Upgrading dynamic placeholders that for the key [$($placeholderKey)]" `
-CurrentOperation "Updating [$($item.Name)]..."
$itemPath = $_.FullPath
$itemId = $_.Id
$version = $_.Version
Get-Rendering -Item $_ -FinalLayout -Placeholder $placeholderKey | Foreach-Object {
# Double check that this is an old style dynamic placeholder - should have a lowercase guid at the end of the string
$matches = [regex]::Matches($_.Placeholder,'(_[0-9a-f]{8}[-][0-9a-f]{4}[-][0-9a-f]{4}[-][0-9a-f]{4}[-][0-9a-f]{12})')
if ($matches.Success) {
Write-Host "Updating: " $itemPath
$oldPlaceholderKey = $_.Placeholder
Write-Host "Old placeholder: " $_.Placeholder
Foreach($group in $matches.Groups)
{
$renderingId = $group.Value
# Replace the old style guid with the new style guid and add the unique number
# for this upgrade, that will always be 0
$newPlaceholder = $_.Placeholder.Replace($renderingId, "-{$($renderingId.Replace('_','').ToUpper())}-0")
$_.Placeholder = $newPlaceholder
}
Write-Host "New placeholder: " $_.Placeholder
Set-Rendering -FinalLayout -Item $item -Instance $_
$properties = @{
ITemID = $itemId
Version = $version
ItemPath = $itemPath
RenderingId = $_.UniqueId
OldPlaceholderKey = $oldPlaceholderKey
NewPlaceholderKey = $_.Placeholder
}
$results += New-Object psobject -Property $properties
}
}
}
$Results | Select-Object ItemId,Version,ItemPath,RenderingId,OldPlaceholderKey,NewPlaceholderKey | Export-Csv -notypeinformation -Path $outputFilePath
Write-Progress -Completed -Activity "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment