0

Using Sitecore 10.1.3

We have an existing application which uses both SXA default & custom renderings. There are 100+ renderings and many are not in use any more.

For each rendering, I would check Navigate > Links. For some, I could see the page item reference but some refer to .../Presentation/Available renderings.

And some references are Partial Designs.

So, it is challenging to find out which renderings are actually being used in page items.

Is there a quick way to find out, like a Powershell script.

2 Answers 2

1

If want to know How many times is each rendering used for a specific website. You can use the below Powershell Script with more information for each rendering.

  1. Open PowerShell ISE (You can found this in /sitecore/shell/ menu, Development Tools, PowerShell ISE
  2. Copy past the script below.
  3. Save as "How many times is each rendering used for a specific website" below Script Library/SPE/Reporting/Content Reports/Reports/Solution Audit
<#
    .SYNOPSIS
        How many times is each rendering used in your solution?
         
    .NOTES
        copy from www.stockpick.nl
#>
Import-Function Render-ReportField
 
filter IsRendering {
    # Look for Controller and View renderings
    $renderingIds = @("{2A3E91A0-7987-44B5-AB34-35C2D9DE83B9}","{99F8905D-4A87-4EB8-9F8B-A9BEBFB3ADD6}")
    if(($renderingIds -contains $_.TemplateID)) { $_; return }
}
 
$database = "master"
 
# Renderings Root
$renderingsRootItem = Get-Item -Path "$($database):{32566F0E-7686-45F1-A12F-D7260BD78BC3}"
$websiteRootItem = Get-Item -Path "$($database):{0DE95AE4-41AB-4D01-9EB0-67441B7C2450}"
 
$props = @{
    Parameters = @(
        @{Name="websiteRootItem"; Title="Choose the report root"; Tooltip="Only items from this root will be returned."; }
    )
        Title = "Items With Component Report"
        Description = "Choose the website for the report."
        Width = 650
        Height = 250
        ShowHints = $true
        Icon = [regex]::Replace($PSScript.Appearance.Icon, "Office", "OfficeWhite", [System.Text.RegularExpressions.RegexOptions]::IgnoreCase)
    }
     
    $result = Read-Variable @props
     
    if($result -eq "cancel") {
        exit
}
 
 
$items = $renderingsRootItem.Axes.GetDescendants() | Initialize-Item | IsRendering
 
$reportItems = @()
foreach($item in $items) {
    $count = 0
    $websitecount = 0;
    $referrers = Get-ItemReferrer -Item $item
    if ($referrers -ne $null) {
        $count = $referrers.Count
        foreach($ref in $referrers) {
             
            if ($ref.ItemPath.StartsWith($websiteRootItem.ItemPath)) {
            $websitecount++
            }
        }
    }
 
    $reportItem = [PSCustomObject]@{
        "Icon" = $item."__Icon"
        "Name"=$item.Name
        "UsageCount"=$count
        "WebsiteCount" = $websitecount
        "ItemPath" = $item.ItemPath
        "TemplateName" = $item.TemplateName
        "Controller" = $item.Controller
    }
    $reportItems += $reportItem
}
 
$reportProps = @{
    Property = @(
        "Icon",@{Name="Rendering Name"; Expression={$_.Name}}, 
        @{Name="Total Number of usages"; Expression={$_.UsageCount}},
        @{Name="Number of usages below: "+$websiteRootItem.Name.ToString(); Expression={$_.WebsiteCount}},
        "ItemPath",
        @{Label="Rendering Type"; Expression={$_.TemplateName} },
        "Controller"
    )
    Title = "Custom rendering report"
    InfoTitle = "Available Renderings"
    InfoDescription = "Count of references for each rendering. Results include only MVC Controller and View renderings.for" + $websiteRootItem.ItemPath.ToString()
}
 
$reportItems | 
        Sort-Object WebsiteCount -Descending |
        Show-ListView @reportProps
 
Close-Window

Reference and Credit : https://www.stockpick.nl/english/how-many-times-is-each-rendering-used-for-a-specific-website/

Hope it helps!

1
  • Thank you for sharng. But it seems to not work for SXA "Page Design" field where a page will be assigned a Page Design, which will have some Partial Designs, which in turn will have renderings. The code results shows that a rendering usage is 30 but it is used nowhere in any page. Strange!
    – sukesh
    Feb 7 at 7:49
1

You can use this PowerShell script

write-host 'Running script...'
Set-Location master:\content #this was nessesary in my case, because the original "home" item was deleted and SPE had a bug (now fixed)
$pages = get-item master:\content\MySite\articles | get-childitem -Recurse | where-object { $_.TemplateName -match "Article" }
$device = Get-LayoutDevice -Default
$Results = @();
$DataPath = "C:\MySite\ComponentsInUse.csv"
 
foreach($page in $pages){
 
    $renderings = Get-Rendering -Item $page -Device $device -FinalLayout
 
    foreach($rendering in $renderings){
 
        if($rendering.ItemID -ne $null)
        {
            $renderingItem = Get-Item master: -ID $rendering.ItemID
            if($renderingItem -ne $null)
            {
                $Properties = @{
                    RenderingItemName = $renderingItem.Name
                    RenderingItemID = $renderingItem.ID
                    RenderingItemPath = $renderingItem.Paths.Path
                    UsedOnPage = $page.Name
                    UsedOnPageID = $page.ID
                    UsedOnPagePath = $page.Paths.Path
                }
 
                $Results += New-Object psobject -Property $Properties
            }
        }
 
    }
}
 
$Results | Select-Object RenderingItemName,RenderingItemID,RenderingItemPath,UsedOnPage,UsedOnPageID,UsedOnPagePath | Export-Csv -notypeinformation -Path $DataPath
 
write-host 'Script ended'

here is a reference https://codebuildplay.wordpress.com/2016/11/15/sitecore-powershell-how-to-create-a-list-of-renderings-in-use/

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.