Home Gitesh Portfolio Blog About Me Gallery Contact

Sitecore: Powershell query to get a unique list of components used on pages.

With this powershell command you will be to get a unique list of components used on pages.


 

Import-Function -Name ConvertTo-Xlsx
 
$allRendering = Get-ChildItem -Path "master:\layout\Renderings" -Recurse | 
    Where-Object { $_.TemplateName -like "*rendering*"} | 
    Select-Object Name, ID, TemplateName,  Cacheable | 
    Sort-Object -Property Name 
$rootPath = 'master:/sitecore/content/Home'
$itemsToProcess = Get-ChildItem -Path $rootPath -Recurse 
$lists = @()
 
foreach($item in $itemsToProcess) 
{
    $itemRenderings =   Get-Rendering -Item $item -FinalLayout 
 
        foreach($itemRendering in $itemRenderings){    
        $renderingItem = $allRendering | Where-Object { $_.ID -eq $itemRendering.ItemID }
 
        $value = "0"
        
        foreach($list in $lists) 
        {            
            if($list -eq $renderingItem.Name)
            {
                $value = "1"
                # write-host $list  + "Value is 1"
            }
        }
           
        if($value -eq "0")
        {
            if($renderingItem -ne $null)
            {
                write-host $renderingItem.Name
                $lists +=  $renderingItem.Name
            }
        }
 
        # write-host $renderingItem.Name
    
        }    
} 

Posted: 20/10/2022 5:25:47 p.m. by Gitesh Shah | with 0 comments