Pages

Tuesday, October 27, 2009

SharePoint List and ListTemplate

If you ever need to find the list template associate with a list you can do so by accessing the list property “TemplateFeatureId”. This property is of type guid and if a list doesn’t have the list template then value is Guid.Empty.

How to find a list template’s id?

Say you have a list template “CustomerListTemplate” and you need to find out the list template id. You can do so by getting the current SPWeb’s list template collection. Then you can loop through the list templates to find the “CustomerListTemplate”. This is shown in the following code snippet.

            //get all list templates in current site.

            SPListTemplateCollection listTemplates = SPContext.Current.Site.GetCustomListTemplates(SPContext.Current.Web);

            Guid myTemplateId = Guid.Empty;

 

            //loop throug all templates and find your template id

            foreach (SPListTemplate template in listTemplates)

            {

            //if template is hidden or unique then skip

            if (template.Unique || template.Hidden)

                continue;

 

                if (template.Name.Equals("CustomerListTemplate"))

                    myTemplateId = template.FeatureId;

                break;

            }

I have a list template, how I’ll find the lists created by this template?

At first you need to know the template’s ID which is described in section “How to find a list template’s id?”. Once you have your list template’s id you can loop through all lists in your site and compare the list’s “TemplateFeatureId” with the list template id you have for your list template.

foreach (SPList list in SPContext.Current.Web.Lists)

            {

                if (list.TemplateFeatureId == myListTemplateId)

                {

                    //this list is created by your template

                }

            }

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.