How to remove duplicate dynamic placeholders from Final Layout using SPE?

Sitecore Asked by Amitabh Vyas on November 27, 2021

We got duplicate dynamic placeholders assigned in Final Layout. How can I remove the duplicates from the Final Layouts in all the languages?

This is how we are filtering out the placeholders matching the regex. In $matches we get the collection of all the dynamic placeholders, the next step is to remove the duplicates and I want to avoid the nested looping to check and delete:

Get-Rendering -Item $_ | ForEach-Object {
    $rendering = $_;
    $matches = [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}}-0)')

    if ($matches.Success) {
        $newPlaceholder = $rendering.Placeholder

        $matches | ForEach-Object {
            $renderingId = $_.Value

enter image description here

One Answer

Firstly, you can filter the renderings returned before you get to a ForEach-Object, by using a Where-Object (my regex skills are limited, so I've just copied yours):

$Renderings =  Get-Rendering -Item $CurrentItem -FinalLayout | Where-Object {$_.Placeholder -match '(-{[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}}-0)'}

Then you can build an array of your duplicate criteria (in your case the combination of placeholder and datasource). If a value is already in the array, you can remove the rendering.

$UniqueValues = @()
$Renderings | ForEach-Object{
    $Identifier = -join($_.Datasource, $_.Placeholder)
        If($UniqueValues -Contains $Identifier){
            Remove-Rendering -Item $CurrentItem -Instance $_

            $UniqueValues ,+= $Identifier


Answered by James Walford on November 27, 2021

Add your own answers!

Related Questions


0  Asked on October 18, 2020 by katlego-masanabo


Multiroot Treelist with datasource queries

1  Asked on September 26, 2020 by meenu-chauhan


Error processing block: Core.block.StartEnvironmentMinions

0  Asked on September 9, 2020 by eric-gonzalez


Error when trying to configure JSS connected Demo

1  Asked on July 21, 2020 by himanshu-agarwal


Ask a Question

Get help from others!

© 2023 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP