How do I create a Sharepoint list via a feature receiver and an existing list template


I have a list template in a MOSS List Template Gallery and I need to create a list using this template from a feature receiver. This sounds really easy but I cannot find a way of doing this.

SPWeb has a GetCatalog method that returns an SPList with 1 item - my template - but it is an SPListItem and I need an SPListTemplate. How can I 'convert' the item to the correct type?


Use the GetCustomListTemplates method of the SPSite object to get the SPListTemplate object representing your custom template. Then use the SPListCollection.Add method to create a new list from this template. In code this would look something like this:

using (SPSite site = new SPSite("http://server/sites/site"))
using (SPWeb web = site.OpenWeb())
  SPListTemplateCollection templates = site.GetCustomListTemplates(web);
  SPListTemplate template = templates["MyTemplates"];
  Guid listId = web.Lists.Add("Title", "Description", template);