Create List in Sharepoint 2010 Programmatically

There are two ways in creating custom list programmatically

  • Create the list by using the SharePoint Object Model
  • Create the list using an Existing list template

Creating a List programmatically by using the Object Model            

using (SPSite siteCollection = new SPSite(SPContext.Current.Site.Url.ToString()))
{
using (SPWeb spWeb = siteCollection.OpenWeb())
{
spWeb.AllowUnsafeUpdates = true;

spWeb.Lists.Add(“ListName”, “List Discription”, SPListTemplateType.PictureLibrary);

//You can change list template type (SPListTemplateType) ” Tasks, Calendar, Document Library etc.” as per your requirements, all OOTB list templates are listed here

 
spWeb.Update();
spWeb.AllowUnsafeUpdates = false;
}
}

Creating a List programmatically with Custom List Template Types

using (SPSite siteCollection = new SPSite(SPContext.Current.Site.Url.ToString()))
{
using (SPWeb spWeb = siteCollection.OpenWeb())
{
spWeb.AllowUnsafeUpdates = true;

//Get the list template from custom lists created on the SPWeb

SPListTemplate listTemplates = spWeb.ListTemplates[“your list template“];

spWeb.Lists.Add((“ListName”, “List Discription”, listTemplates);

spWeb.Update();
spWeb.AllowUnsafeUpdates = false;
}
}

SharePoint 2010 SPListTemplateType object

List Template Types available in the SharePoint SPListTemplateType object

Out of the box list templates

Name                                            BaseType                                  
Events GenericList
Contacts GenericList
Tasks GenericList
PictureLibrary DocumentLibrary
DiscussionBoard GenericList
Survey Survey
DocumentLibrary DocumentLibrary
Links GenericList
Document Library DocumentLibrary
Announcements GenericList
GenericList GenericList
WorkflowHistory GenericList
DataConnectionLibrary DocumentLibrary
DataSources DocumentLibrary
GanttTasks GenericList
CustomGrid GenericList
NoCodeWorkflows DocumentLibrary
XMLForm DocumentLibrary
WebPageLibrary DocumentLibrary
WorkflowProcess GenericList
Form Library DocumentLibrary
wiki DocumentLibrary
Wiki Page Library DocumentLibrary
Picture Library DocumentLibrary
Picture DocumentLibrary
Contacts GenericList
Calendar GenericList
Discussion Board GenericList
Project Tasks GenericList
Issue Tracking Issue
Custom List GenericList
Custom List in Datasheet View GenericList
Survey Survey
Custom Workflow Process GenericList
Languages and Translators GenericList
Translation Management Library DocumentLibrary
DataSources DocumentLibrary
Data Connection Library DocumentLibrary
Workflow History GenericList
No Code Workflows DocumentLibrary