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;
}
}