Wednesday 9 July 2014

Find all the items that are referring to particular item in Sitecore

9 comments
In this blog post I am going to explain how to get all the items (referrers) which are referring to a particular item (may be template, sublayout or rendering etc.) in Sitecore. There are few ways to achieve this goal:
  1. Using Sitecore Content Editor UI
  2. Writing custom C# code
  3. Using Sitecore Powershell Console Module
  • Using Sitecore Content Editor UI: The simplest way of seeing these referrer links is to go to that particular item and go to the Navigation Strip and then click on Links. This should show you all the items (referrers) that point to this current item and all the items that the current items points to (references).
  • Writing custom C# code: Below function GetReferrers(string itemId) takes item id as input parameter and return the list of items which are pointing to this particular item.
    public Item[] GetReferrers(string itemId)
            {          
                Item item = Sitecore.Data.Database.GetDatabase("master").GetItem(new Sitecore.Data.ID(itemId));
                // getting all linked Items that refer to the Item
                ItemLink[] itemLinks = Globals.LinkDatabase.GetReferrers(item);
                if (itemLinks == null)
                {
                    return null;
                }
                else
                {
                    ArrayList items = new ArrayList(itemLinks.Length);
                    foreach (ItemLink itemLink in itemLinks)
                    {
                        // comparing the database name of the linked Item
                        if (itemLink.SourceDatabaseName == "master")
                        {
                            Item linkItem = Sitecore.Data.Database.GetDatabase("master").Items[itemLink.SourceItemID];
                            if (linkItem != null)
                            {
                                items.Add(linkItem);
                            }
                        }
                    }
                    return (Item[])items.ToArray(typeof(Item));
                }
            }
    
  • Using Sitecore Powershell Console Module: Download Sitecore Powershell Console Module from Sitecore MarketPlace and install the package. Go to Start > Development Tools > PowerShell ISE.

    Enter below powershell script in script field and execute.
    $props = @{
       InfoTitle = "Referrers"
        InfoDescription = "Lists all items that are using this item"
        PageSize = 25
    }
    
    function Get-ItemReferrers {
        $item = Get-Item -Path "/sitecore/templates/Launch Sitecore/Job Function"
        $linkDb = [Sitecore.Globals]::LinkDatabase
        $links = $linkDb.GetReferrers($item)
        foreach($link in $links){
            $linkedItem = Get-Item -Path master:\ -ID $link.SourceItemID
            $linkedItem
        }
    }
    
    $items = Get-ItemReferrers
    $items | Show-ListView @props -Property @{Label="Name"; Expression={$_.DisplayName} },
                @{Label="Updated"; Expression={$_.__Updated} },
                @{Label="Updated by"; Expression={$_."__Updated by"} },
                @{Label="Created"; Expression={$_.__Created} },
                @{Label="Created by"; Expression={$_."__Created by"} },
                @{Label="Path"; Expression={$_.ItemPath} }
    
    Close-Window
    
Above approaches require that Link Database should be up to date. Link database should be updated automatically but you can force rebuild command manually. Go to Start > Control Panel > Databases > Rebuild Link Database
Sitecore Powershell References:
  1. Blog by Adam Najmanowicz 
  2. Blog by Michael West 
  3. How to create custom report using PowerShell blog by Mike Reynolds
Comments and suggestions are most welcome. Happy coding!

9 comments :