Home Gitesh Portfolio Blog About Me Gallery Contact

PowerShell: Find Sitecore Personalization Rules on items in Sitetree

Learn how to use a handy PowerShell script to quickly pinpoint Sitecore items with renderings that have active personalization rules. Personalization in Sitecore allows you to tailor content based on various criteria, and this script simplifies the task of locating items that leverage these rules
 

function HasRuleOnRendering {
    param(
        [Sitecore.Layouts.RenderingDefinition]$Rendering
    )

    $hasRules = $false

    if($rendering -and ![string]::IsNullOrEmpty($rendering.Rules) ) {
        $hasRules = $true
    }

    $hasRules
}

# Specify the new root item path where you want to start the search
$rootItemPath = "/sitecore/content"

# Create an array to store rendering information with personalization rules
$renderingInfo = @()

# Get all items under the specified root item path
$items = Get-Item -Path $rootItemPath | Get-ChildItem -Recurse

# Loop through each item and check if any rendering has personalization rules enabled
foreach ($item in $items) {
    $renderings = Get-Rendering -Item $item -FinalLayout
    foreach ($rendering in $renderings) {
        
        # Check if personalization is not null before attempting to get rules
        if ((HasRuleOnRendering -Rendering $rendering)) {
            $renderingItem = Get-Item -Path "master:" -ID $rendering.ItemID
            $renderingName = $renderingItem.Name
            
            # Create a custom object to store rendering information with personalization rules
            $renderingObject = [PSCustomObject]@{
                ItemName      = $item.Name
                ItemPath      = $item.FullPath
                RenderingName = $renderingName
            }

            # Add the rendering information to the array
            $renderingInfo += $renderingObject
        }
    }
}

# Display the rendering information with personalization rules in a table format
$renderingInfo | Format-Table -AutoSize



Keywords:

Sitecore, PowerShell, Personalization, renderings
Posted: 23/01/2024 2:57:12 p.m. by Gitesh Shah | with 0 comments