Sitecore Powershell Script to Update Datasource Path In Final Rendering
In project, there was a requirement to update datasource for specific rendering so to update the rendering datasource I wrote below PowerShell script.
Function Change-RenderingDatasourceInAllPages{
Param(
[Parameter(Mandatory=$true)] [String]$PagesRoot,
[Parameter(Mandatory=$true)] [String]$Placeholder,
[Parameter(Mandatory=$true)] [String]$DataSourcePath,
[Parameter(Mandatory=$true)] [String]$ItemId
)
$pages = @(get-item $PagesRoot)
$pages += @(get-childitem -Path $PagesRoot -Recurse)
$defaultLayout = Get-LayoutDevice "Default"
foreach($page in $pages)
{
Get-Rendering -Item $page -Device $defaultLayout -FinalLayout -Placeholder $Placeholder | ForEach {
if($_.ItemID -eq $ItemId)
{
$_.Datasource=$DataSourcePath
Set-Rendering -Item $page -Instance $_ -FinalLayout:$True
write-host 'datasource updated for' $page.Id
}
}
}
}
#Function call
Change-RenderingDatasourceInAllPages -PagesRoot "master:/sitecore/content/Home" -Placeholder "/header/topnav-right" -DataSourcePath "/sitecore/content/Data/Header" -ItemId "{D19659D4-8D05-4FF4-9A77-BE61AD9A5781}"
Below are the parameters which you need to change:
- PagesRoot: This is the path of a page, from where it will start to check the rendering’s datasource which will be appended with database name like master:/sitecore/content/Home
- Placeholder: This is a placeholder name where rendering is applied already like “main” placeholder
- DataSourcePath: This is a datasource path, which we are going to update
- ItemId: This is a item id of rendering.
Enjoy coding :)
Comments