How To Create A Document Library In SharePoint 2013 Programmatically

Welcome to a blog on how to create a Document Library in SharePoint 2013 programmatically, using a Console Application. We will use Visual Studio to create a Document Library in SharePoint 2013 site.

Let’s see, how to do it.
  • Open your Visual Studio.
  • Select New Project.

     
  • Select Console Application.
  • Add the references, Microsoft.SharePoint dll and Microsoft.SharePoint.Client dll.
  • Paste the code, given below, under Program.cs.
Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Security;  
  5. using System.Net;  
  6. using System.Text;  
  7. using System.Web;  
  8. using System.Data;  
  9. using Microsoft.SharePoint;  
  10. using Microsoft.SharePoint.Client;  
  11. namespace ConsoleApplication1 {  
  12.     class Program {  
  13.         static void Main(string[] args) {  
  14.             //get the web  
  15.             ClientContext context = new ClientContext("Provide your site url");  
  16.             Web web = context.Web;  
  17.             //provide the document library information as Title and Description  
  18.             ListCreationInformation createLibrary = new ListCreationInformation();  
  19.             createLibrary.Title = "My Doc Lib";  
  20.             createLibrary.Description = "My Document Library";  
  21.             //choose a template as Document Library.  
  22.             createLibrary.TemplateType = (int) ListTemplateType.DocumentLibrary;  
  23.             //add library to SharePoint  
  24.             web.Lists.Add(createLibrary);  
  25.             context.ExecuteQuery();  
  26.             Console.WriteLine("Document Libarary Created");  
  27.             Console.ReadKey();  
  28.         }  
  29.     }  
  • Run the code and your Document Library will be created.