Pages

October 8, 2015

How to know which lists uses a specific Template ID

Suppose you are in a situation where you see the following xml schema for a list item event receiver:


<Receivers ListTemplateId="16016">
<Receiver>
<Name>CustomAlertEventReceiver</Name>
<Type>ItemUpdating</Type>
<Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
<Class>CustomAlertEventReceiver.ItemEventReceiver.ItemEventReceiver</Class>
<SequenceNumber>10000</SequenceNumber>
</Receiver>
</Receivers>


How do you figure which list or lists the event receiver applies to?

Well, we can know if this Template ID is OOB from this list.

Also, we can use the browser if we want to check only one or 2 specific lists that we already know they exist. This post describes how to get the template ID of from list settings page using the 'g_wsaListtemplateId' variable.
http://sharepoint.stackexchange.com/questions/89030/2010-custom-list-template-id

But the stuff above do not really answer the question as the id '16016' is custom.. "I want to know all -if any- lists that uses this template ID across the whole farm"?


My answer: Let's write some code!
I wrote a simple console app to get a collection of list names that are based on a specific template ID, and their web url.
Make sure you run this code in a SharePoint server machine because it will access the local farm and iterate through web applications, site collections & webs.


Hope this helps someone!


public struct TemplateIDResult
    {
        private string _ListName;
        private string _WebUrl;
        public string ListName
        {get { return _ListName; } set { _ListName = value; }}
        public string WebUrl
        {get { return _WebUrl; } set { _WebUrl = value; }}
    }
class Program
    {
        static void Main(string[] args)
        {
            int TargetTemplateTypeVal = 16016;// a custom id
            List<TemplateIDResult> ResultsFound = new List<TemplateIDResult>();
            ResultsFound.AddRange(getTemplateTypeFoundInFarm(TargetTemplateTypeVal));
            if (ResultsFound.Count > 0)
            {
                Console.WriteLine("Results count: " + ResultsFound.Count.ToString());           
                foreach (TemplateIDResult result in ResultsFound)
                {
                    Console.WriteLine(string.Format("List name: '{0}' in Web: {1}" ,result.ListName, result.WebUrl));
                } 
            }
            Console.WriteLine();
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
        private static List<TemplateIDResult> getTemplateTypeFoundInFarm(int TemplateTypeValue)
        {
            List<TemplateIDResult> result = new List<TemplateIDResult>();
            SPWebService service = SPFarm.Local.Services.GetValue<SPWebService>(string.Empty);
            SPWebApplicationCollection WebAppColl = service.WebApplications;
            foreach (SPWebApplication WA in WebAppColl)
            {
                result.AddRange(getTemplateTypeFoundInWebApp(WA, TemplateTypeValue));
            }
            return result;     
        }
        private static List<TemplateIDResult> getTemplateTypeFoundInWebApp(SPWebApplication WA, int TemplateTypeValue)
        {
            List<TemplateIDResult> result = new List<TemplateIDResult>();
            SPSiteCollection AllSiteColl = WA.Sites;           
            foreach (SPSite site in AllSiteColl)
            {
                #region Site
                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    using (SPSite s = new SPSite(site.ID))
                    {
                        try
                        {
                            SPWebCollection AllWebs = site.AllWebs;
                            foreach (SPWeb web in AllWebs)
                            {
                                #region Web
                                int TemplateTypeVal;
                                foreach (SPListTemplate template in web.ListTemplates)
                                {
                                    TemplateTypeVal = (int)(template.Type);
                                    if (TemplateTypeVal == TemplateTypeValue)
                                    {
                                        TemplateIDResult r = new TemplateIDResult();
                                        r.ListName = template.Name;
                                        r.WebUrl = web.Url;
                                        result.Add(r);
                                    }
                                }
                                web.Dispose();
                                #endregion
                            }
                        }
                        catch (Exception e){                            Console.WriteLine("Web access error inside: " + site.Url);}
                    }
                });
                #endregion
            }           
            return result;
        }
    }