TransWikia.com

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 $_

        }
        else{
            $UniqueValues ,+= $Identifier
        }

}

Answered by James Walford on November 27, 2021

Add your own answers!

Ask a Question

Get help from others!

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