This blog will help you in modifying the rendering parameter for a rendering using PowerShell script. While working on a project, one might face a scenario where there is a need to make changes in a rendering parameter of a particular rendering.
The rendering might have been used on multiple pages on a website.
In this case, it will be very tedious for a content author to change the parameter for a rendering on multiple pages. This script might be helpful here as we can identify the rendering and modify the required parameters with minimal effort.
In the following example, I am taking a scenario where we must replace a style parameter added to a rendering with another style. A style Style1 is added to a Container rendering, and we need to replace this style with Style2.
To achieve this, we need to use 3 ids in the script.
1. ID1 – The style id which is applied to rendering (Style1 id {ID1})
2. ID2 – The style id which we need to change (Style2 id {ID2})
3.ItemID – The item under which we have multiple pages on which the rendering is applied
function ModifyRenderingParameter($item, $fieldID) { $styleArray = [System.Collections.ArrayList]@() $laytoutField = New-Object "Sitecore.Data.Fields.LayoutField" $item.Fields[$fieldID] $modified = $false $definition = [Sitecore.Layouts.LayoutDefinition]::Parse($laytoutField.Value) $definition.Devices | % { $_.Renderings | % { $par = [Sitecore.Web.WebUtil]::ParseUrlParameters($_.Parameters) foreach($styleIds in $par["Styles"]){ $ids = $styleIds.split('|'); foreach($id in $ids){ if($id -eq '{ID1}'){ $styleArray = $ids.replace($id, "{ID2}") -join "|" if ($styleArray -ne $null) { $par["Styles"] = $styleArray $_.Parameters = (New-Object "Sitecore.Text.UrlString" $par).GetUrl() $modified = $true } } } } } } if ($modified) { $itemList.Add($item); $item.Editing.BeginEdit() $laytoutField.Value = $definition.ToXml() $item.Editing.EndEdit() > $null } } $item = Get-Item -Path "master:" -ID "{ItemID}" $allItems = Get-ChildItem -Item $item -Recurse $itemList = [System.Collections.ArrayList]@() foreach($item in $allItems){ ModifyRenderingParameter $item ([Sitecore.FieldIDs]::LayoutField) ModifyRenderingParameter $item ([Sitecore.FieldIDs]::FinalLayoutField) } $itemList | Show-ListView -Property Name, ProviderPath, Language
In the end, you will receive the list of items affected during execution.
Comment below if you have any suggestions or questions.
Nice one.