How To Create A List/Library Programmatically In SharePoint 2013

Here I am explaining about list and library creation using SharePoint Server Object model and Client Object Model in SharePoint. 
 
Introduction

Microsoft has already provided out of box features to create a list or library in SharePoint. If we want more customization then we need the custom coding to do this.
 
We can create a list in many ways .
  1. Using Server Object Model ( Visual Studio Required )
  2. Using Client Object Model
  3. Using Out Of Box Features 
Using Server Object Model ( Visual Studio Required )
 
We need to install Visual Studio in our machine for creating a List using SharePoint Server Object Model. The Server Object Model will be executed in the server side & it provides a rich set of classes in representing & manipulating SharePoint objects. Server object model is like production server environment to access the data where Sharepoint is installed on machine .
 
Server Object Model is the most extensive API set available for SharePoint 2013. The core assembly is Microsoft.SharePoint.dll which is installed in the Global Assembly Cache.

The general classes are available in the Microsoft.SharePoint namespace and the administration classes inside the Microsoft.SharePoint.Administration namespace. 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using Microsoft.SharePoint.Client;  
  8.    
  9. namespace WebApplication  
  10. {  
  11.     public partial class ListCreation : System.Web.UI.Page  
  12.     {  
  13.         protected void Page_Load(object sender, EventArgs e)  
  14.         {         
  15.    
  16.             using (SPSite oSPsite = new SPSite("https://sharepoint.com/sites/TestSite"))  
  17.             {  
  18.    
  19.                 using (SPWeb oSPWeb = oSPsite.OpenWeb())  
  20.                 {  
  21.                     oSPWeb.AllowUnsafeUpdates = true;  
  22.                     /*create list from custom ListTemplate present within ListTemplateGalery */  
  23.                     SPListTemplateCollection lstTemp = oSPsite.GetCustomListTemplates(oSPWeb);  
  24.                     SPListTemplate template = lstTemp["custom template name"];  
  25.                     oSPWeb.Lists.Add("TestList""Description", template);  
  26.                     SPList newList = web.Lists["TestList"];  
  27.                     oSPWeb.AllowUnsafeUpdates = false;  
  28.    
  29.                 }  
  30.    
  31.             }  
  32.    
  33.         }  
  34.     }  
  35. }  
Create a List using Javascript 
 
The JavaScript object model is defined in a set of *.js files located at %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\15\TEMPLATE\LAYOUTS on each server. 
 
We need to add the reference file in the top of the page, 
  1. <script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>  
  2. <script type="text/javascript" src="/_layouts/15/sp.core.js"></script>  
  3. <script type="text/javascript" src="/_layouts/15/sp.js"></script>  
  1. <script type="text/javascript">    
  2. function createList()     
  3. {    
  4.     var siteUrl="https://sharepoint.com/sites/TestSite";    
  5.     var clientContext = new SP.ClientContext(siteUrl);    
  6.     var oWebsite = clientContext.get_web();    
  7.     alert(oWebsite);    
  8.     var listCreationInfo = new SP.ListCreationInformation();    
  9.     listCreationInfo.set_title('TestList');    
  10.     listCreationInfo.set_templateType(SP.ListTemplateType."custom template name");    
  11.     this.oList = oWebsite.get_lists().add(listCreationInfo);    
  12.     clientContext.load(oList);    
  13.     clientContext.executeQueryAsync(    
  14.     Function.createDelegate(thisthis.onQuerySucceeded),     
  15.     Function.createDelegate(thisthis.onQueryFailed)    
  16. );    
  17. }    
  18.     
  19. function onQuerySucceeded()     
  20. {    
  21.     alert("result");    
  22.     var result = oList.get_title() + ' created.';    
  23.     alert(result);    
  24. }    
  25.     
  26. function onQueryFailed(sender, args)     
  27. {    
  28.     alert('Request failed. ' + args.get_message() +     
  29.     '\n' + args.get_stackTrace());    
  30. }    
  31. </script>    
Create List Using Client Object Model
 
There are two assemblies to be referred to for working with the Client Object Model.
  • Microsoft.SharePoint.Client.dll
  • Microsoft.SharePoint.Client.Runtime.dll
These assemblies can be found in the 15 Hive folder: %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\15\ISAPI.
  1. using System.Collections.Generic;    
  2. using System.Linq;    
  3. using System.Text;    
  4. using System.Threading.Tasks;    
  5. using Microsoft.SharePoint.Client;    
  6. namespace CreateTestList     
  7. {    
  8.     class Program     
  9.     {    
  10.         static void Main(string[] args)    
  11.         {    
  12.             // ClientContext - Get the context for the SharePoint Site      
  13.     
  14.     
  15.             ClientContext clientContext = new ClientContext("https://sharepoint.com/sites/TestSite");    
  16.             // Specifies the properties of the new custom list      
  17.     
  18.             ListCreationInformation creationInfo = new ListCreationInformation();    
  19.             creationInfo.Title = "TestList";    
  20.             creationInfo.Description = "Create TestList";    
  21.             creationInfo.TemplateType = (int) ListTemplateType.GenericList;    
  22.             // Create a new custom list      
  23.     
  24.             List newList = clientContext.Web.Lists.Add(creationInfo);    
  25.             // Retrieve the custom list properties      
  26.             clientContext.Load(newList);    
  27.             // Execute the query to the server.      
  28.             clientContext.ExecuteQuery();    
  29.             // Display the custom list Title property      
  30.             Console.WriteLine(newList.Title);    
  31.             Console.ReadLine();    
  32.         }    
  33.     }    
  34. }    
Using Out Of Box Features
 
Go to Your Site -> Add an app-> Create a Custom List
 
SharePoint

Click on Custom List -> New popup will come -> Enter the Name of your List -> Create

SharePoint

Next look into the below image, Your newly created list is visible over here.

SharePoint
These are the process for create a list in SharePoint 2013,2010 or SharePoint Online.
 
Same you can also create Document Library in SharePoint Online Using Server and Client Object model plus Out of Box features.
  
Using Server Object Model ( Visual Studio Required )
 
Server Object Model is the most extensive API set available for SharePoint 2013. The core assembly is Microsoft.SharePoint.dll which is installed in the Global Assembly Cache.

The general classes are available in the Microsoft.SharePoint namespace and the administration classes inside the Microsoft.SharePoint.Administration namespace. 
  1. SPSite mySite = new SPSite("https://sharepoint.com/sites/TestSite");  
  2. SPWeb myWeb = mySite.openWeb();  
  3. myWeb.AllowUnSafeUpdates = true;  
  4. myWeb.Lists.Add("TestLibrary","Test Document Library", SPListTemplateType.DocumentLibrary);  
  5. myWeb.Update();  
  6. myWeb.AllowUnsafeUpdates = false;  
  7. myWeb.Dispose();  
  8. mySite.Dispose();  
Create a Library using Javascript
 
The JavaScript object model is defined in a set of *.js files located at %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\15\TEMPLATE\LAYOUTS on each server.

We need to add the reference file in top of the page. 
  1. <script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>  
  2. <script type="text/javascript" src="/_layouts/15/sp.core.js"></script>  
  3. <script type="text/javascript" src="/_layouts/15/sp.js"></script>  
  4. <script type="text/javascript">  
  5.     var context = SP.ClientContext.get_current();  
  6.     var web = context.get_web();  
  7.     var list = web.get_lists();  
  8.     var docLibCreation;  
  9.   
  10.     function createDocLib() {  
  11.         docLibCreation = new SP.ListCreationInformation();  
  12.         docLibCreation.set_title("TestLibrary"); //list title    
  13.         docLibCreation.set_templateType(SP.ListTemplateType.documentLibrary); //document library type    
  14.         list.add(docLibCreation)  
  15.         context.load(list);  
  16.         context.executeQueryAsync(onDocLibCreationSuccess, onDocLibCreationFail);  
  17.     }  
  18.   
  19.     function onDocLibCreationSuccess() {  
  20.         alert(docLibCreation.title + "Created");  
  21.     }  
  22.   
  23.     function onDocLibCreationFail(sender, args) {  
  24.         alert('Failed to Create the Document Library. Error:' + args.get_message());  
  25.     }  
  26. </script>  
Create Library Using Client Object Model
 
There are two assemblies to be referred to for working with the Client Object Model.
  • Microsoft.SharePoint.Client.dll
  • Microsoft.SharePoint.Client.Runtime.dll
These assemblies can be found in the 15 Hive folder: %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\15\ISAPI.
  1. using (ClientContext clientCTX = new ClientContext(url))  
  2. {  
  3.      ListCreationInformation Information = new ListCreationInformation();  
  4.      Information.Description = "Test Document Library";  
  5.      Information.Title = "TestLibrary";  
  6.      Information.TemplateType = 101;  
  7.      List newLib = clientCTX.Web.Lists.Add(Information);  
  8.      clientCTX.Load(newLib);  
  9.      clientCTX.ExecuteQuery();  
  10. }  
Using Out Of Box Features
 
Go to Your Site -> Add an app-> Create a Document Library

SharePoint 
Click on Document Library -> New popup will come -> Enter the Name of your Library-> Create

SharePoint

Next look into the below image, Your newly cretaed library is visible over here.
SharePoint
These are all the steps for creating a List and Library In SharePoint.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now