logo
Published on

Component Rendering Variants Usage

Authors

This PowerShell script generates a report displaying the usage of component rendering variants in a SXA Sitecore solution. I find it helpful as it identifies the rendering variants highly used in the solution as well as the ones that haven't been used at all.

The script can be useful during maintenance routines and solution clean up by easily identifying what is safe to be removed without compromising the solution.

<# 
    .SYNOPSIS 
    Count of references for each rendering variant definition. 
    Results include only Rendering Definitions under Rendering Variants. 
#> 

$props = @{
    Title = "Available Rendering Variants Definition"
    Description = "Count of references for each rendering variant definition. Results include only Rendering Definitions under Rendering Variants."
    OkButtonName = "Run Report"
    CancelButtonName = "Cancel"
    Parameters = @(
        @{ Name = "rootItem"; Title = "Search Root"; Editor = "droptree"; Source = "/sitecore/content" }
    )
}

$result = Read-Variable @props
if($result -ne "ok") {
    Close-Window
    Exit
}

Import-Function Render-ReportField

filter IsRenderingDefinition {
    # Template: /sitecore/templates/Foundation/Experience Accelerator/Rendering Variants/Variant Definition 
    $renderingIds = @("{FB3E3034-33F8-4CE8-BE98-DD05010F4C22}")

    if(($renderingIds -contains $_.TemplateID)) { $_; return }
}

$database = "master"

# Root location
$renderingsRootItem = Get-Item -Path "$($database):$($rootItem.ID)"

$items = $renderingsRootItem.Axes.GetDescendants() | Initialize-Item | IsRenderingDefinition

$reportItems = @()

foreach($item in $items) {
    $count = 0
    $referrers = Get-ItemReferrer -Item $item

    if ($referrers -ne $null) {
        $count = $referrers.Count
    }

    $reportItem = [PSCustomObject]@{
        "Icon" = $item."__Icon"
        "Name"=$item.Name
        "UsageCount"=$count
        "ItemPath" = $item.ItemPath
        "TemplateName" = $item.TemplateName
        "Controller" = $item.Controller
        }

    $reportItems += $reportItem
}

$reportProps = @{
    Property = @(
        "Icon",
        @{Name="Rendering Definition Name"; Expression={$_.Name}},
        @{Name="Number of usages"; Expression={$_.UsageCount}}, 
        "ItemPath",
        @{Label="Rendering Type"; Expression={$_.TemplateName}}
    )

    Title = "Available Rendering Variants Definition"
    InfoTitle = "Available Rendering Variants Definition"
    InfoDescription = "Count of references for each rendering variant definition. Results include only Rendering Definitions under Rendering Variants." 

}

$reportItems |
    Sort-Object UsageCount -Descending |
    Show-ListView @reportProps

Close-Window

When the script is launched, it will ask for the root path to start the query from. Only items using the variant definition template /Sitecore/Templates/Foundation/Experience Accelerator/Rendering Variants/Variant Definition will be filtered.

Report can run from the Variants root folder to display all component rendering variants or per component for more specific output.

select starting location

The report will display a table with the rendering variant name, the number of usages, and the path to the component rendering variant.

select starting location

The report above shows the results obtained after querying the Cards component displaying its rendering variants.