Sunday 7 September 2014

Find all templates which are not referenced

6 comments
Recently I was doing some cleanup of Sitecore content tree and I’ve got into a situation where I have to find all templates that are not being used by any Sitecore item i.e find all templates which are not referenced. The simplest way of seeing these referrer links is to go to that particular template and go to the Navigation Strip and then click on Links. This should show you all the items (referrers) that point to this selected template and all the items that the selected template points to (references).  This activity needs to be done manually for each template thus I’ve decided to write custom code which returns a list of all the templates that have "no referrers".  Below function GetUnReferencedTemplates() returns list of templates that are not being used by any Sitecore item or have no referrers.
public List<Item> GetUnReferencedTemplates()
        {
            List<Item> UnReferencedTemplates = new List<Item>();

            // Get all templates which are based on /sitecore/templates/System/Templates/Template
            //Template id of /sitecore/templates/System/Templates/Template = {AB86861A-6030-46C5-B394-E8F99E8B87DB}

            string queryTemplateId = "fast:/sitecore/templates//*[@@templateId='{AB86861A-6030-46C5-B394-E8F99E8B87DB}']";

            //Alternatively you can also use @@templatename='Template'
            //string queryTemplateName = "fast:/sitecore/templates//*[@@templatename='Template']";

            Item[] getTemplates = Sitecore.Context.Database.SelectItems(queryTemplateId);

            if (getTemplates.Length > 0 && getTemplates != null)
            {
                foreach (Item item in getTemplates)
                {
                    // getting all linked Items that refer to the particular template item
                    ItemLink[] itemLinks = Globals.LinkDatabase.GetReferrers(item);
                    if (itemLinks == null || itemLinks.Length == 0)
                    {
                        UnReferencedTemplates.Add(item);
                    }
                }
            }
            return UnReferencedTemplates;
        }
Below Sitecore Fast Query will get all templates which are based on /sitecore/templates/System/Templates/Template. Template id of /sitecore/templates/System/Templates/Template is {AB86861A-6030-46C5-B394-E8F99E8B87DB}
string queryTemplateId = "fast:/sitecore/templates//*[@@templateId='{AB86861A-6030-46C5-B394-E8F99E8B87DB}']";
I’ve modified Sitecore Fast Query a bit to search all templates under /sitecore/templates/User Defined context node location in order to optimize the performance. Also I don’t want to fetch system templates which are under /sitecore/templates/System location.
string queryTemplateId = "fast:/sitecore/templates/User Defined//*[@@templateId='{AB86861A-6030-46C5-B394-E8F99E8B87DB}']";
Execution of above method might take some time as it depends on structure of your Sitecore Content tree. Comments and suggestions are most welcome. Happy coding!

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

6 comments :

  1. item.Editing.EndEdit(); leading to infinite loop even after adding SynchronizedCollection

    ReplyDelete
  2. Chrome for OSX instructions??

    ReplyDelete
  3. if i will configure it in server so it will work in another user browser or not ?

    ReplyDelete
  4. Edit the ~/Library/Preferences/com.google.Chrome.plist and add a new array.

    Key: EnableDeprecatedWebPlatformFeatures
    Type: Array

    Key: Item 0
    Type: String
    Value: ShowModalDialog_EffectiveUntil20150430



    I used Xcode to edit the plist file

    ReplyDelete
  5. thanks. but after editing and restart. no luck yet

    ReplyDelete
  6. That's odd your's looks exactly like mine does. Have you checked in chrome://policy that everything's ok?

    ReplyDelete