This blog introduces you to concept of updating rendering and rendering parameters using Sitecore PowerShell Extension. If you are maintaining a web application based on Sitecore with loads of content items, you may be familiar with content cleanup or update requests that comes from business. This is where Sitecore PowerShell Extension comes very handy and saves our life.
A little background ..
We were using Media Framework based connector for Brightcove videos integration in Sitecore 8.2 website. When we upgraded to Sitecore 10.1, connector for Brightcove videos integration was also migrated from Media Framework to Data Exchange Framework. So far so good. But we realized that Media Framework based integration involved storing some video and video player related data in rendering parameters as in snapshot below -
We did not see these rendering parameters getting used anymore in Data Exchange Framework based integration. Hence, it was decided to remove these rendering parameters from renderings.
The Solution..
We started developing a PowerShell script which can do the job. So I needed some commands which can get me the rendering I am looking for. Below commands do the job -
Basically, first we get the rendering item present in Renderings folder which we want to look up in page items
$renderingItem = Get-Item -Path "master" -ID <item ID for rendering in Renderings folder goes here>
Now, we have the rendering item that we need to look into pages. So, we use below command to get instances of rendering item from the page item.
Note: Below command will return an array of items or an item depending on the whether the rendering has multiple instance on the page or single instance on the page.
$rendering = Get-Rendering -Item $item -Rendering $renderingItem -FinalLayout -Device $defaultLayout
Next you need to find if this rendering instance has any rendering parameters in it. Below command helps with that. This is true only if rendering parameters have some value -
if($params = $rendering.Parameters)
And then you need to remove these renderings from the page item. We do this by setting Parameters to null as below -
$rendering.Parameters = $null;
Finally, we set the updated rendering on the page item using below command -
Set-Rendering -Item $item -Instance $rendering -FinalLayout -Device $defaultLayout
Collectively, if you need to run the script in a part of content tree, the script will look like this -
$currentDatabase = $SitecoreContextItem.Database.Name
$siteRoot = "/sitecore/content/<path to your Site or item whose children you want to crawl>/";
$pageBaseTemplateID = "<base page template ID to identify page items>"
$RenderingID = "<item ID of the rendering from which you want to remove rendering parameters>"
$defaultLayout = Get-LayoutDevice "Default"
#below line gets only the page items from content tree
$items = @(Get-ChildItem -Path $siteRoot -Language * -Recurse) | Where-Object { [Sitecore.Data.Managers.TemplateManager]::GetTemplate($_).InheritsFrom($pageBaseTemplateID) }
if ($items -ne $null -and $items.Count -gt 0) {
$renderingItem = Get-Item -Path "master" -ID $RenderingID
New-UsingBlock (New-Object Sitecore.Data.BulkUpdateContext){
foreach($item in $items)
{
if($rendering = Get-Rendering -Item $item -Rendering $renderingItem -FinalLayout -Device $defaultLayout)
{
if($rendering -is [system.array]){ #a page item can have multiple rendering instances
foreach($renderingInstance in $rendering){
if($params = $renderingInstance.Parameters)
{
Write-Messages -Message "Removing parameters from $($item.Paths.Path) for $($renderingInstance.UniqueID)"
Write-Messages -Message "Parameters Value :: $($params)"
$renderingInstance.Parameters = $null;
Set-Rendering -Item $item -Instance $renderingInstance -FinalLayout -Device $defaultLayout
}
}
}
elseif($params = $rendering.Parameters)
{
Write-Messages -Message "Removing parameters from $($item.Paths.Path)"
Write-Messages -Message "Parameters Value :: $($params)"
$rendering.Parameters = $null;
Set-Rendering -Item $item -Instance $rendering -FinalLayout -Device $defaultLayout
}
}
}
}
}
else {
Write-Messages -Message "Content tree crawl complete; no items found.";
exit
}
In this example, we have removed rendering parameters from a rendering. You can do other updates too like updating the datasource etc. by similar approach.
It is always fun to work with PowerShell scripts in Sitecore. Hope it helps you. Thanks for reading!!
Comments
Post a Comment