Home Gitesh Portfolio Blog About Me Gallery Contact

Sitecore: Powershell query to find circular references of items in your content tree.

With this powershell command you will be to find circular references of items in your content tree.

Cicular references means if item A is having a field pointing to Item B and Item B has a field which poitning to Item A. This can sometimes create an endless loop and can break your site.

In our site this endless loop happened in JSON layout rendering. We were not even using the JSON rendering layout but was used by SXA OOTB.

 

function Get-ItemBasedOnTemplate {
    param(
        [string]$TemplateId
    )
 
    $queue = New-Object System.Collections.Queue
    $processedLookup = New-Object System.Collections.Generic.HashSet[string]
    if(-not(Test-Path -Path "master:$($TemplateId)")) { return }
    $processedLookup.Add($TemplateId) > $null  
    Get-ItemReferrer -Id $TemplateId -ItemLink | 
        Where-Object { $_.SourceItemID } | 
        ForEach-Object { $queue.Enqueue($_.SourceItemID) }
 
    $database = Get-Database -Name "master"
    while($queue.Count -and ($referrerId = $queue.Dequeue())) {
        if($processedLookup.Contains($referrerId)) { continue }
        $processedLookup.Add($referrerId) > $null
        $referrer = $database.GetItem($referrerId)
        if(!$referrer) { continue }
        if($referrer.Paths.FullPath.StartsWith("/sitecore/templates")) { 
            if($referrer.Name -eq "__Standard values") { continue }
            foreach($referrerItemLink in Get-ItemReferrer -Id $referrerId -ItemLink | Where-Object { $_.SourceItemID }) {
                $queue.Enqueue($referrerItemLink.SourceItemID)
            }
            $itemTemplate = [Sitecore.Data.Managers.TemplateManager]::GetTemplate($referrerId, $database)
        } else {
            $itemTemplate = [Sitecore.Data.Managers.TemplateManager]::GetTemplate($referrer)
        }
 
        if ($itemTemplate -and $itemTemplate.DescendsFromOrEquals($TemplateId)) {
            $referrer
        }
    }
}
$items = @()
$projectTemplates = Get-ChildItem -Path "master://sitecore/templates/Project/projectName" -Recurse  | Where-Object { $_.TemplateName -eq 'Template' }
$featureTemplates = Get-ChildItem -Path "master://sitecore/templates/Feature/projectName" -Recurse  | Where-Object { $_.TemplateName -eq 'Template' }
$templates = $projectTemplates + $featureTemplates
 
foreach($template in $templates)
{
    $path= "master:/"+$template.Paths.FullPath
    $fields = Get-ChildItem -Path $path -Recurse | Where-Object { $_.Type -eq "Multilist" -or $_.Type -eq "Treelist" -or $_.Type -eq "TreelistEx"}
 
    foreach ($field in $fields) 
    {
      $items = Get-ItemBasedOnTemplate -TemplateId $template.ID | Select-Object -Property Name, ID, ProviderPath, TemplateName
      foreach($item in $items)
      {
          $contentItem = Get-Item -Path master: -ID $item.ID
          
          if (![string]::IsNullOrEmpty($contentItem[$field.Name]) -and $contentItem[$field.Name].Contains($item.ID)) 
          {
              $contentItem
              $contentItem[$field.Name]
              $field.Name
              write-host "------------------------------------------------------------------------------------------"               
          }
      }
    }
}

Posted: 30/08/2022 8:39:42 p.m. by Gitesh Shah | with 0 comments