1

There is a page template called X being used to create a number of pages. There are two renderings A and B being used on many pages that created from X page template.

Now I want to set the one rendering parameter value in Rendering B from Rendering A's datasource value. I tried to set the standard value for Rendering parameter template, however, during debugging the paramter vaule is empty.

I am unable to set the rendering parameter value. I am currently using SPE 5.0 so, Set-RenderingParameter -Parameter $parameters command is not working.

Below is the script I am using,

write-host 'Running script...'
$rootItem = Get-Item "master:/sitecore/content/test";
$pageTemplate = Get-Item "{D2AA238D-E7BB-48D6-1111-29BBB66B454E}";
$pages=Get-ChildItem $rootItem.FullPath -recurse | Where-Object { $_.TemplateName -eq $pageTemplate.Name }
$device = Get-LayoutDevice -Default
$Results = @();    

function Set-RenderingParametersForField($item) {
        $modified = $false
    $renderingItem= Get-Rendering -Item $item -Device $device -FinalLayout 
     $bRenderingItem= $renderingItem| Where-Object -FilterScript {$_.ItemID -EQ '{21BD2323-711F-4826-AB23-999996FFB31C}'}
    
     if($bRenderingItem -ne $null)
     {
 #$par = [Sitecore.Web.WebUtil]::ParseUrlParameters($_.Parameters)
            #$renderingParameter = $par["RENDERING_PARAMETER_FIELD_NAME"]
         $parameters = @{"Param3"="Test"}
         Set-RenderingParameter -Parameter $parameters
           #Get-RenderingParameter -Rendering $bRenderingItem | Format-Table -Auto
     }
     
}
 
foreach($page in $pages){
 
    $renderings = Get-Rendering -Item $page -Device $device -FinalLayout
 
    foreach($rendering in $renderings){
 
        if($rendering.ItemID -ne $null)
        {
            $aRenderingItem = Get-Item master: -ID $rendering.ItemID | Where-Object -FilterScript {$rendering.ItemID -EQ '{8E52CDC4-AA69-41DA-AAAA-D3187C581666}'}
            if($aRenderingItem -ne $null)
            {
                if($rendering.Datasource -ne $null -and $rendering.Datasource -eq '{C27D10EA-246F-4583-A3D0-3350DD17FFFF}')
                {
                   Set-RenderingParametersForField $page
                }
            }
        }
 
    }
}
 

write-host 'Script ended'

UPDATE:

Updated the function to get this working,

function Set-RenderingParametersForField($item , $fieldID) {
    $laytoutField = New-Object "Sitecore.Data.Fields.LayoutField" $item.Fields[$fieldID]
    $modified = $false
    $definition = [Sitecore.Layouts.LayoutDefinition]::Parse($laytoutField.Value)
   $definition.Devices | % {
        $_.Renderings | Where-Object -FilterScript {$_.ItemID -EQ '{21BD2323-711F-4826-AB23-999996FFB31C}'}| % {
            $par = [Sitecore.Web.WebUtil]::ParseUrlParameters($_.Parameters)
            $renderingParameter = $par["RENDERING_PARAMETER_FIELD_NAME"]
            if ($renderingParameter -ne $null) {
                $par["RENDERING_PARAMETER_FIELD_NAME"] = "TEST"
                $_.Parameters = (New-Object "Sitecore.Text.UrlString" $par).GetUrl()
                $modified = $true
            }
        }
        
    }
    if ($modified) {
        $item.Editing.BeginEdit()
        $laytoutField.Value = $definition.ToXml()
        $item.Editing.EndEdit() > $null
    }
}

3 Answers 3

0

You need to update your code slightly to this.

Set-RenderingParameter -Parameter $parameters | 
    Set-Rendering -Item $item -FinalLayout

Then it will update the rendering parameters on the item.

I have verified it on my local and it is working fine for me.

So your function should look like this.

function Set-RenderingParametersForField($item) {
        $modified = $false
    $renderingItem= Get-Rendering -Item $item -Device $device -FinalLayout 
     $bRenderingItem= $renderingItem| Where-Object -FilterScript {$_.ItemID -EQ '{21BD2323-711F-4826-AB23-999996FFB31C}'}
    
     if($bRenderingItem -ne $null)
     {
 #$par = [Sitecore.Web.WebUtil]::ParseUrlParameters($_.Parameters)
            #$renderingParameter = $par["RENDERING_PARAMETER_FIELD_NAME"]
         $parameters = @{"Param3"="Test"}
         Set-RenderingParameter -Parameter $parameters | 
Set-Rendering -Item $item -FinalLayout
           #Get-RenderingParameter -Rendering $bRenderingItem | Format-Table -Auto
     }
     
}

Find more details here.

https://doc.sitecorepowershell.com/appendix/presentation/set-renderingparameter

1
  • Set-RenderingParameter works with SPE 5.1 and not with SPE 5.0. I am using 5.0. So this was not working in my end. Commented Nov 2, 2022 at 8:39
0

You can use two approaches for this

Approach 1:

Using this approach we get all the items by language and template, traverse the items and do a string replacement on the __Final Renderings field value. This approach gets the job done. However, it has to check the __Final Rendering field of every item to see if the string to be replaced exists or not. Depending on the number of items you need to update it might be rather slow.

$items = Get-ChildItem -Path "master:/sitecore/content/Home" -Recurse | Where-Object { $_.TemplateName -eq 'Page Template Name' }

foreach ($item in $items) {
  $sharedRendering = $item.Fields["__Renderings"].Value
   $finalRendering = $item.Fields["__Final Renderings"].Value
    if($sharedRendering -Like '*{000000-0000-0000-0000-0000000}*') {
    $replaced = $finalRendering -replace '{1111-1111-1111-1111-1111111}', '{692E7341-9421-4ACE-8310-6D15E5EEBE52}'
      $item.Editing.BeginEdit()
       $item.Fields["__Final Renderings"].Value = $replaced
      $item.Editing.EndEdit()
   
 }

Approach 2:

This approach is rather more elegant as it uses the SPE Get-Rendering command to retrieve the rendering instance and the Get-RenderingParameter command to retrieve the parameters for the instance. Once you have the parameters you can modify them then use Set-RenderingParameter and Set-Rendering to save the parameters and the rendering. This approach gives you fine-grained control over the renderings and parameters that are updated as the filtering allows you to target renderings used within specific placeholders, using specific parameters, etc.

$rendering = Get-Item -Path "master:\sitecore\layout\Sublayouts\Sample Sublayout"

$items = Get-ChildItem -Path "master:\sitecore\templates\Sample Item" -Recurse 
foreach($item in $items) {
    $renderingInstance = Get-Rendering -Item $_ -Rendering $rendering 
    if ($renderingInstance) { 
        Set-Rendering -Item $_ -Instance $renderingInstance -Parameter @{ 
            "Lorem" = "Ipsum" 
        } 
        Write-Host "Updated $($_.Paths.FullPath)" 
    } 
}

Hope this helps you.

0

I was able to resolve the issue by setting the rendering parameter in the standard value of the page template. This way Rendering parameter was available in all the items. And updated the Set-RenderingParametersForField function to get this working,

function Set-RenderingParametersForField($item , $fieldID) {
    $laytoutField = New-Object "Sitecore.Data.Fields.LayoutField" $item.Fields[$fieldID]
    $modified = $false
    $definition = [Sitecore.Layouts.LayoutDefinition]::Parse($laytoutField.Value)
   $definition.Devices | % {
        $_.Renderings | Where-Object -FilterScript {$_.ItemID -EQ '{21BD2323-711F-4826-AB23-999996FFB31C}'}| % {
            $par = [Sitecore.Web.WebUtil]::ParseUrlParameters($_.Parameters)
            $renderingParameter = $par["RENDERING_PARAMETER_FIELD_NAME"]
            if ($renderingParameter -ne $null) {
                $par["RENDERING_PARAMETER_FIELD_NAME"] = "TEST"
                $_.Parameters = (New-Object "Sitecore.Text.UrlString" $par).GetUrl()
                $modified = $true
            }
        }
        
    }
    if ($modified) {
        $item.Editing.BeginEdit()
        $laytoutField.Value = $definition.ToXml()
        $item.Editing.EndEdit() > $null
    }
}
1
  • Add your answer here well the code part, so that if anyone else is facing the issue they can see it here. Commented Nov 2, 2022 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.