17 February 2009

Programmatically show all list templates for a SharePoint web

To programmatically create lists in a SharePoint web, I found you have to look for list templates in two different locations.

The first (obvious) location is in the ListTemplates collection on the SPWeb object, but then u would mis the custom list templates stored in the List Template Gallery.

The code belows shows all list templates for a web.

using(SPSite site = new SPSite("http://demosite"))
{
    using(SPWeb web = site.OpenWeb())
    {
        Console.WriteLine("==== List Templates");
        foreach(SPListTemplate template in web.ListTemplates)
        {
            Console.WriteLine(template.Name);
        }
  
        Console.WriteLine("==== List Templates from the List Template Gallery");
        foreach (SPListTemplate template in site.GetCustomListTemplates(web))
        {
            Console.WriteLine(template.Name);
        }
    }
}

No comments:

Post a Comment