Create List Programmatically in SharePoint

Often there is a requirement of creating Lists within a SharePoint website programmatically using API calls. Described below are 2 different methods of achieving the same using C#.Net as the programming language. In the first approach, a SharePoint List gets created using an existing custom List Template present within the List Template Gallery. This approach helps replicate a list exactly in the same structure as of the list from which the custom list template was generated.

In the second approach, a blank SharePoint List gets generated depending on the List Template Type specified during the creation process. This approach can be used to create blank lists of type Announcements, Contacts, Discussion Board, Document Library, Events, Links, Meetings, Picture Library, Survey, Tasks, etc.

using (SPSite oSPsite = new SPSite("http://Web URL"))
{
    oSPsite.AllowUnsafeUpdates = true;
 
    using (SPWeb oSPWeb = oSPsite.OpenWeb())
    {
        oSPWeb.AllowUnsafeUpdates = true;
 
        /* 1. create list from custom ListTemplate present within ListTemplateGalery */
        SPListTemplateCollection lstTemp = oSPsite.GetCustomListTemplates(oSPWeb);
        SPListTemplate template = lstTemp["custom template name"];
        oSPWeb.Lists.Add("List Name", "Description", template);
 
 
        /* 2. create list from sharepoint list content type (e.g. Links) */
        oSPWeb.Lists.Add("List Name", "Description", SPListTemplateType.Links);
 
        oSPWeb.AllowUnsafeUpdates = false;
    }
 
    oSPsite.AllowUnsafeUpdates = false;
}
150 150 Burnignorance | Where Minds Meet And Sparks Fly!