2

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.

2
  • Is this what you are looking for - sitecore.stackexchange.com/questions/18846/… Commented Apr 23, 2021 at 7:35
  • No, I have gone through that post, My requirement here is I just need to update the personalization rule from target item to source and child if that rendering is already available.
    – Mouni
    Commented Apr 23, 2021 at 7:41

1 Answer 1

0

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.

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.