Create Sharepoint List Programmaticaly

In this article I am explaining how to create Sharepoint List Programmatically.

To create any object in sharepoint, you generally need to call Add() method on the container into which you will be placing the new item.

To create new Sharepoint List
Open Visual studio - File - Create New project
then select C# as the language and Console Application from the panel.
Give CreateList as name for project, then click ok.


After Creating the project, you need to add the Sharepoint DLL for your project.
For this right click on solution Explorer and select Add Reference . Browse for Microsoft.Sharepoint.dll file and click ok.
With default installation of Sharepoint, you can find this dll file at below location.

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI\Microsoft.SharePoint.dll

So, you successfully establish the environment to create List in Sharepoint.
The code to create Sharepoint List is shown below.


    
using System; using System.Collections.Generic; using System.Text; using Microsoft.SharePoint; namespace CreateList { class Program { static void Main(string[] args) { SPSite site = new SPSite("http://home-pt72rbbzr0"); SPWeb web = site.AllWebs[0]; SPListTemplate temps = web.ListTemplates["Tasks"]; Guid newListGuid = web.Lists.Add("New Task List", "This List Added Programmatically", temps); SPList newList = web.Lists[newListGuid]; Console.WriteLine("List Added Successfully"); Console.ReadLine(); } } }

In the above code, if you observe we created the SPSite object for root site i.e, http://home-pt72rbbzr0 (In my System I have root URL as like this, you may have different root site URL. Please mention your sharepoint root site URL to create site for this root site)

After that I am refereing the first web of the root site by mentioning index as 0. If you want you can mention web name instead of index.
Now we need to have templates for our New List. For this, I am using existing Tasks List. Then we need to create new GUID for our New List, it requires new List name, description and default templates (just we created from the Tasks List).


We write the code to create List, then we need to deploy it on production server.
If the production server is same as develop server, then just press F5, if not you need to copy the entire project to production server and install or deploying your code using various deployment methods in .Net Framework and install on server.


After completing the installation, you will get the following window.

Create Sharepoint List

open new Browser window with the new site URL(i.e. http://home-pt72rbbzr0), and click on "view All Site Contenet" as shown below.

Create Sharepoint List

After that go to All site content dropdown List and select "Lists" field as shown below.

Create Sharepoint List

Then you can observe your new List just we created as shown in figure.

Create Sharepoint List

In this way we can create any sharepoint List with given templates.


Download source code here