Create a SharePoint list programmatically
Create a SharePoint list programmatically
Creating Lists within a SharePoint website programmatically using Visual studio.
we can create 2 different methods of achieving the same using C#.Net as the programming language.
In the first Method is a SharePoint List gets created using an existing custom List Template present within the List Template Gallery. A list exactly in the same structure as of the list from which the custom list template was generated.
In the second Method is 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.
Create custom List using in SharePoint
[csharp]using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Client;
namespace WebApplication4
{
public partial class ListCreation : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
using (SPSite oSPsite = new SPSite("http://splessons:1515/"))
{
using (SPWeb oSPWeb = oSPsite.OpenWeb())
{
oSPWeb.AllowUnsafeUpdates = true;
/*create list from custom ListTemplate present within ListTemplateGalery */
SPListTemplateCollection lstTemp = oSPsite.GetCustomListTemplates(oSPWeb);
SPListTemplate template = lstTemp["custom template name"];
oSPWeb.Lists.Add("Splessons", "Description", template);
SPList newList = web.Lists["Splessons"];
oSPWeb.AllowUnsafeUpdates = false;
}
}
}
}
}[/csharp]
Create custom List using in Existing Template
[csharp]using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Client;
namespace WebApplication4
{
public partial class ListCreation : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
using (SPSite oSPsite = new SPSite("http://splessons:1515/"))
{
using (SPWeb oSPWeb = oSPsite.OpenWeb())
{
oSPWeb.AllowUnsafeUpdates = true;
SPListTemplateCollection lstTemp = oSPsite.GetCustomListTemplates(oSPWeb);
/* create list from sharepoint list content type (e.g. calender) */
oSPWeb.Lists.Add("SplessonsCanlender", "Description", SPListTemplateType.Calender);
SPList newList = web.Lists["SplessonsCanlender"];
oSPWeb.AllowUnsafeUpdates = false;
}
}
}
}
}
[/csharp]