Create a Document Library in SharePoint 2013 Using CSOM-JavaScript

Introduction

This article explains how to create a Document Library in SharePoint 2013 using CSOM-JavaScript.

Prerequisites

  1. Ensure you have access to the Office 365 online.
  2. Ensure Napa tool is available in your site.

Use the following procedure.

  1. Create an app for SharePoint using Office 365 Tools. If you have missed out on how to create an app in SharePoint 2013, then Click here.
     
  2. Click on the Default.aspx page.


     
  3. Add Title and MyCustomField text boxes and a button to create an item and place it inside the “PlaceHolderMain” tag as in the following:
      
    <button id="btnCreateDocLib" onclick="createDocLib()">Create Document Library</button><br/>
     
  4. Click on the App.js file.


     
  5. Globally declare the content, web, list and listCreation objects as shown below.

    var context = SP.ClientContext.get_current();
    var web=context.get_web();
    var list=web.get_lists();
    var docLibCreation;
     
  6. Now write the function to create the list in the site.

    function createDocLib() {
    docLibCreation = new SP.ListCreationInformation();
    docLibCreation.set_title("MyDocumentLibrary"); //list title
    docLibCreation.set_templateType(SP.ListTemplateType.documentLibrary); //document library type
    list.add(docLibCreation)
    context.load(list);
    context.executeQueryAsync(onDocLibCreationSuccess, onDocLibCreationFail);
    }
     
  7. Here in this example we are creating the Document Library using the ListCreationInformation object.
     
  8. Using the ListCreationInformation object you can set the Title, Template Type and so on.
     
  9. After creating the ListCreationInformation object we need to add this docLibCreaion object to the list object and then load the list object and execute the code by calling the executeQueryAsync () method.

    function onDocLibCreationSuccess() {
    alert(docLibCreation.title + "Created");
    }
    function onDocLibCreationFail(sender, args) {

    alert('Failed to Create the Document Library. Error:' + args.get_message());
    }
     
  10. We can use the following templates to create the lists/libraries:

    SP.ListTemplateType.GenericList
    SP.ListTemplateType.DocumentLibrary
    SP.ListTemplateType.Survey
    SP.ListTemplateType.Announcements
    SP.ListTemplateType.Contacts
    SP.ListTemplateType.Events
    SP.ListTemplateType.Tasks
    SP.ListTemplateType.DiscussionBoard
    SP.ListTemplateType.PictureLibrary
    SP.ListTemplateType.DataSources
    SP.ListTemplateType.XmlForm
    SP.ListTemplateType.NoCodeWorkflows
    SP.ListTemplateType.WorkflowProcess
    SP.ListTemplateType.WebPageLibrary
    SP.ListTemplateType.CustomGrid
    SP.ListTemplateType.WorkflowHistory
    SP.ListTemplateType.GanttTasks
    SP.ListTemplateType.IssuesTracking

    That's it!! Now you will be able to create the Document Library.

Testing

  1. Now to run the app click on the "Play" button that is available towards the left-most corner.


     
  2. The app is packaged, deployed and installed on your Office 365 Site.


     
  3. Now you will be able to see the following page.


     
  4. Click on the “Create Document Library” button.
  5. To see the Document Library that was created, just go to the following URL:

    Syntax: https://yoursite/DocLibName
    Example: https://mysite/MyDocumentLibrary


Summary

Thus in this article you saw how to create a Document Library in SharePoint 2013 using CSOM-JavaScript.