Freigeben über


How to create list using custom list template using SharePoint object model

Requirement:

  • I have a custom list template uploaded to the List template gallery of the site
  • How can I create a new list whose template is custom list template using SharePoint object model

Sample Code:

Decription: GetCustomListTemplates(...) method of SPSite object gives the collection of the custom lists.
Just a note: ListTemplates property of SPWeb object returns ONLY the OOB list templates ( as a SPListTemplateCollection). It DOES NOT include the custom list templates (which are stored in the List Template gallery on the site.)
Note:

  1. Change the highlighted value in the below sample code appropriately.
  2. The title column value & NOT the name column value given to the custom list template (in the List Template Gallery) needs to be used in calling methods.

SPSite osite = new SPSite("https://anjalich11:2020");
SPWeb oweb = osite.OpenWeb();
SPListTemplateCollection listtempcol = oweb.Site.GetCustomListTemplates(oweb);
oweb.Lists.Add("TestList", "This is my list ", listtempcol["TestListSTP"]);
SPList olist = oweb.Lists["TestList"];
olist.OnQuickLaunch = true;
olist.Update();
oweb.Dispose();
osite.Dispose();

Comments

  • Anonymous
    November 19, 2009
    good., great.. solved my problem.. thanks :).