Introduction
This article explains how to create a Document Library in SharePoint 2013 using CSOM-JavaScript.
Prerequisites
- Ensure you have access to the Office 365 online.
- Ensure Napa tool is available in your site.
Use the following procedure.
- 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.
- Click on the Default.aspx page.
- 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/>
- Click on the App.js file.
- 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;
- 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);
}
- Here in this example we are creating the Document Library using the ListCreationInformation object.
- Using the ListCreationInformation object you can set the Title, Template Type and so on.
- 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());
}
- 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
- Now to run the app click on the "Play" button that is available towards the left-most corner.
- The app is packaged, deployed and installed on your Office 365 Site.
- Now you will be able to see the following page.
- Click on the “Create Document Library” button.
- 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.