I want to update the personalization rule for all the pages which have one common rendering.
Is it possible to identify the rendering using Powershell and update the personalization rule to the target page and subpages?
Thank you.
I want to update the personalization rule for all the pages which have one common rendering.
Is it possible to identify the rendering using Powershell and update the personalization rule to the target page and subpages?
Thank you.
Below is the PowerShell script to achieve it :
Import-Module -Name SitecorePowerShellExtensions
$rootPath = "sitecore/content/Home"
$renderingName = "YourRenderingName"
$newPersonalizationRule = "<rule>Your personalization rule here</rule>"
function Update-PersonalizationRule {
param(
[Parameter(Mandatory=$true)]
[string]$itemPath
)
$item = Get-Item -Path $itemPath
if ($item -ne $null) {
$renderingsField = [Sitecore.Layouts.RenderingReference[]]$item["__Renderings"]
foreach ($rendering in $renderingsField) {
if ($rendering.RenderingItemPath -eq $renderingName) {
$rendering.Rules = $newPersonalizationRule
$item.Editing.BeginEdit()
$item["__Renderings"] = $renderingsField
$item.Editing.EndEdit()
Write-Output "Updated personalization rule for item: $itemPath"
}
}
}
}
function Process-Items {
param(
[Parameter(Mandatory=$true)]
[string]$parentPath
)
$items = Get-ChildItem -Path $parentPath -Recurse
foreach ($item in $items) {
Update-PersonalizationRule -itemPath $item.ItemPath
}
}
Process-Items -parentPath $rootPath
Set $rootPath
to the root of the content tree where you want to start the search.
Set $renderingName
to the name or ID of the rendering you are looking for.
Define $newPersonalizationRule
with the XML or string representation of the new personalization rule you want to apply.