0

I am trying to retrieve these values found in the "final renderings" field: p:after and p:before.

But I can't seem to find them anywhere. Is there a way to get them out?

The reason I need those, is I am taking some renderings, that are nested inside another "columns" rendering and pulling them out. Unfortunately this means they are getting placed differently than before, as the "columns" rendering has the p:after placement set. I would like to transfer this from the "columns" rendering to the first nested rendering.

Simplified PowerShell script I am testing:

$rootItem = Get-Item master:"/content";

$renderings = @('{521F66ED-76BC-4C40-9D8D-9FC23145FC80}', # Container deck 3 column
            '{8278046D-8762-4F1F-916C-07ECEC2EE9C7}') # Virksom 3 column deck

$defaultLayout = Get-LayoutDevice "Default"

foreach($renderingId in $renderings) {

$oldRendering = Get-Item -Path "master:" -ID $renderingId

Get-ChildItem $rootItem.FullPath -recurse -Language "da" | ForEach-Object {
    $itemPath = $_.Paths.ContentPath
    $sourceRenderings = Get-Rendering -Item $_ -Device $defaultLayout -FinalLayout | Where-Object { $_.ItemID -eq $renderingId -And -Not([string]::IsNullOrEmpty($_.Datasource))  }
    
    if ($sourceRenderings -ne $null) {

        foreach($sourceRendering in $sourceRenderings) {
            $column1 = Get-Rendering -Item $_ -Device $defaultLayout -FinalLayout | Where-Object { $_.Placeholder -eq "/main/virksom-content/deck-column-1-$($sourceRendering.UniqueID)-0"  }
            foreach($col in $column1)
            {
                #tried to find it here
                #Write-Host ($col| Format-List | Out-String)
                
                #tried to find it here, probably the same as before
                $finalLayoutField = $_.Fields[[Sitecore.FieldIDs]::FinalLayoutField]
                $finalLayoutXml = [Sitecore.Data.Fields.LayoutField]::GetFieldValue($finalLayoutField)
                $finalLayout = [Sitecore.Layouts.LayoutDefinition]::Parse($finalLayoutXml)
                $renderings = $finalLayout.Devices[0].Renderings
                Write-Host ($renderings| Format-List | Out-String)
                
                #nothing in Dynamic Properties
                #foreach($dynamicProperty in $col.DynamicProperties)
                #{
                    #$propertyName = $dynamicProperty.ToString()
                    #$text = $dynamicProperty.Value
                    #Write-Host("$($propertyName) - $($text))");
                #}
                
                # new placeholder, that is no the old dynamic placeholder
                $placeholder = $col.Placeholder
                $placeholderLength = $placeholder.Length
                $placeholderLastIndexOfSlash = $placeholder.LastIndexOf('/')
                $placeholderAncestorPath = $placeholder.SubString(0, $placeholderLastIndexOfSlash)
                
                #save the new placeholder
                #but we need the p:after on the old "columns" component
                # $col.Placeholder = $placeholderAncestorPath;
                # Set-Rendering -Item $_ -Instance $col -FinalLayout;
                
                #delete old rendering
                #missing
            }                
        }
    }
}
}
2
  • what is your code?
    – Marek Musielak
    Jul 20, 2022 at 7:58
  • @MarekMusielak I have added a script example, but my question was more general, how can i get and set "p:after" and "p:before" via SPE
    – Holger
    Jul 20, 2022 at 8:55

1 Answer 1

1

What you can update in your code is to first get the value of the Final Rendering Field (That you are already doing). Here is an example.

$rootItem = Get-Item -Path "master:" -ID "{B485F716-9E95-42E8-8DE0-844E988A1BDF}"
$layoutfield = $rootItem.Fields["__Final Renderings"].value

And then cast this value into XML.

$xmlData =  [xml]($layoutfield)

So your XML will become like this. (I used an online tool to convert string raw value into HTML to see how it looks)

<?xml version="1.0" encoding="UTF-8"?>
<r xmlns:p="p" xmlns:s="s" p:p="1">
    <d id="{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}">
        <r uid="{67FD787F-0E4F-4F59-9EC9-67AC9A5F588D}" p:before="*" s:ds="{C0B095E6-4F35-4208-8209-AAB9446A7BD7}" s:id="{3972C278-7E7A-4C2B-A293-66AF860F5C3A}" s:par="Reset Caching Options&amp;GridParameters=%7B908E2BC6-C110-4ED7-AF39-7EEACBB31A34%7D&amp;FieldNames=%7B4D608918-2AB2-48BC-AE4E-A8886991E6E3%7D&amp;Styles&amp;CacheClearingBehavior=Clear%20on%20publish&amp;RenderingIdentifier&amp;DynamicPlaceholderId=2" s:ph="footer" />
        <r uid="{9D547A21-EE39-4A78-9652-84CF9DB532E9}" p:after="r[@uid='{67FD787F-0E4F-4F59-9EC9-67AC9A5F588D}']" s:ds="local:/Data/carousel" s:id="{5A21C781-8E74-4DD0-8261-8CCDA1BF0454}" s:par="Navigation=%7B848DFE42-2B13-430E-A0B9-89CE25730A06%7D&amp;Transition=%7BD4B706CC-7EEC-4DB9-A87D-123B29803490%7D&amp;Timeout=2000&amp;PauseOnHover=1&amp;Reset Caching Options&amp;GridParameters=%7B908E2BC6-C110-4ED7-AF39-7EEACBB31A34%7D&amp;Styles&amp;CacheClearingBehavior=Clear%20on%20publish&amp;RenderingIdentifier&amp;DynamicPlaceholderId=1" s:ph="main" />
        <r uid="{83732FD0-718A-4637-AC13-84940164A9C9}" s:ds="" />
    </d>
</r>

Loop through your XML like this and update the value of p:after and p:before with your value using SetAttribute function.

foreach($node in $nodes) {
   $node.SetAttribute("p:after", "your-id")
}

Finally using this command you will get back your updated XML into a string.

$xmlData.OuterXml

Update this string raw value into your Final Rendering Field.

And That's it.

Hope this will help you.

2
  • Did it solve your issue? Jul 20, 2022 at 12:32
  • This brought close to a solution, though I felt it need more steps and was quite cumbersome compared to updating the Placeholder. I had hope that they could be accessed like the other attribues in SPE.
    – Holger
    Jul 21, 2022 at 12:39

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.