Sitecore 9.x upgrade and dynamic placeholders

In the previous Sitecore versions dynamic placeholders where not there out of the box, so in case you needed them a custom solution was required. Good news came with Sitecore 9.x as that was integrated and ready to be used OOTB.

That’s great yes, but, there is always a “but” when it comes to solutions upgrade, the way we were generating the dynamic placeholders key could potentially be different to what Sitecore is doing now with the builtin feature.

One of the most common approaches was to use the solution based on the DynamicPlaceholders.Mvc nuget package, you might be familiar with the approach that Habitat solution was following:

@using Sitecore.Feature.Teasers
@using Sitecore.Foundation.SitecoreExtensions.Extensions
@using Sitecore.Foundation.Theming.Extensions
@using Sitecore.Mvc.Presentation
@model Sitecore.Feature.Teasers.Models.DynamicTeaserModel

@using (Html.BeginEditFrame(Model.Item.ID.ToString(), ""))
{
  var options = RenderingContext.Current.Rendering.GetCarouselOptions();
  <div class="owl-carousel" data-options='{"items": @options.ItemsShown, "navigation": @(options.ShowNavigation ? "true" : "false"), "navigationText": ["<", ">"], "autoPlay": @(options.AutoPlay ? "true" : "false") }'>
    @for (var i = 0; i < Model.Items.Count(); i++)
    {
      <div>
        @Html.Sitecore().DynamicPlaceholder($"teaser-placeholder-{i + 1}")
      </div>
    }
  </div>
  }

All good, so now the problems! After we finished with the upgrade, we noted that the content from Dynamic Placeholders was gone. We did some research and noticed that now the generated key was different:

Before we had something like that:

teaser-placeholder_fb746602-bbca-4412-a1c0-4306ff6fc731

And now Sitecore is generating the keys like that:

teaser-placeholder-{FB746602-BBCA-4412-A1C0-4306FF6FC731}-0

Powershell to the rescue!!

We decided to go with what we think is the better approach to work with this kind of bulk content update, powershell. You can find the script here in Github.

function ProcessRendering{
    [CmdletBinding()]
    Param(
    [parameter(Mandatory=$true, Position=0)]
    [Sitecore.Data.Items.Item]
    $item,
    [parameter(Mandatory=$true, Position=1)]
    [Sitecore.Layouts.RenderingDefinition]
    $rendering,
    [parameter(Mandatory=$false, Position=2)]
    [bool]
    $param
 )
	$phMatches = [regex]::Matches($_.Placeholder,'(_[0-9a-fA-F]{8}[-][0-9a-fA-F]{4}[-][0-9a-fA-F]{4}[-][0-9a-fA-F]{4}[-][0-9a-fA-F]{12})')

	if ($phMatches.Success) {
		Write-Host "Match found in item - [$($item.Paths.FullPath)]"
		$replacePh = $rendering.Placeholder
		$phMatches | ForEach-Object {
			$renderingId = $_.Value
			$replacePh = $replacePh.Replace($renderingId, "{$($renderingId.ToUpper())}-0")
		}

		$replacePh = $replacePh.Replace('{_', "-{")
		$rendering.Placeholder = $replacePh
		Write-Host "Replacing [$($rendering.Placeholder)] with [$($replacePh)]"
		Set-Rendering -Item $item -Instance $rendering -FinalLayout:$param
	}
}

$allPaths = "/sitecore/templates", "/sitecore/content"

New-UsingBlock (New-Object Sitecore.Data.BulkUpdateContext) {
	$allPaths | ForEach-Object -Process {
		Get-ChildItem -Path $_ -Version * -Recurse | ForEach-Object {
			$item = $_;
			Write-Host "Processing shared layout for item $($_.ID)" 
			Get-Rendering -Item $item | ForEach-Object {
				ProcessRendering $item $_ $false
			}
			Write-Host "Processing final layout for item $($_.ID)" 
			Get-Rendering -Item $item -FinalLayout | ForEach-Object {
				ProcessRendering $item $_ $true
			}
		}
	}
}

After running it you will get all your dynamic placeholder keys updated on your templates and content items.

I hope it helps you if you face a similar issue.

Happy upgrade!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s