Create Folder in Document Library Using REST

This article shows how to create a folder in the list/document Library using REST. Develop the project using the following Method in the NAPA Tool:

On your Developer Site, open the "Napa" Office 365 Development Tools and then choose Add New Project.

  • Choose the App for SharePoint template, name the project Create Site and then choose the Create button.
  • Replace APP.js with the following source code below.
  • Publish Your App.


Prerequisites: The following are important steps to be done before creating the app. Specify the permissions that your app needs as in the following:

Choose the Properties button at the bottom of the page.

  • In the Properties window, choose Permissions.
  • In the Content category, set Write permissions for the Tenant scope.
  • In the Social category, set Read permissions for the User Profiles scope.
  • Close the Properties window.



Default ASPX: Add the following div content to the default aspx page in the app.

  1. <div>  
  2.     <p>  
  3.         <b>Create Folder</b>  
  4.         <br />  
  5.         <input type="text" value="List Name Here" id="CreateFolder" />  
  6.         <button id="btnclick">Create Folder</button>  
  7.     </p>  
  8. </div> 

CodeUnderstanding



Lib=SharePoint Library Name
Folder B=NewFolder Name


SourceCode

  1. 'use strict';   
  2. var hostweburl;  
  3. var appweburl;   
  4.  // Get the URLs for the app web the host web URL from the query string.  
  5. $(document).ready(function ()  
  6. {   
  7.     //Get the URI decoded URLs.  
  8.     hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));  
  9.     appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));   
  10.     // Resources are in URLs in the form:  
  11.     // web_url/_layouts/15/resource    
  12.          $("#btnclick").click(function (event) {  
  13.             FolderCreation();  
  14.             event.preventDefault();  
  15.         });  
  16.     // Load the js file and continue to load the page with information about the folders.  
  17.     // SP.RequestExecutor.js to make cross-domain requests  
  18.     $.getScript(hostweburl + "/_layouts/15/SP.RequestExecutor.js");  
  19. });  
  20. //Retrieve all of the folders from root Site  
  21. function FolderCreation()
    {  
  22.     var executor;  
  23.     var getfoldername= document.getElementById("CreateFolder").value;  
  24.     // Initialize the RequestExecutor with the app web URL.  
  25.     executor = new SP.RequestExecutor(appweburl);  
  26.     executor.executeAsync({  
  27.   url: appweburl + "/_api/SP.AppContextSite(@target)/web/GetFolderByServerRelativeUrl('lib')/folders?@target='" + hostweburl + "'",  
  28.         method: "POST",  
  29.         body: "{ '__metadata':{ 'type': 'SP.Folder' }, 'ServerRelativeUrl':'Folder B' }",
  30. }
  31.           headers:   
  32.            {  
  33.              "accept""application/json; odata=verbose",  
  34.     "content-type""application/json; odata=verbose"  
  35.             },    
  36. success: FoldersSuccessHandler,  
  37.         error: FoldersErrorHandler  
  38.     });  
  39. }   
  40. //Populate the selectFolders control after retrieving all of the folders.  
  41. function FoldersSuccessHandler(data) {  
  42.     alert("Folder Created successfully in Library");  
  43. }  
  44. function FoldersErrorHandler(data, errorCode, errorMessage) {  
  45.     alert("Could not Create a Folder in  Library: " + errorMessage);  
  46. }  
  47. //Utilities  
  48. // Retrieve a query string value.  
  49. // For production purposes you may want to use a library to handle the query string.  
  50. function getQueryStringParameter(paramToRetrieve) {  
  51.     var params = document.URL.split("?")[1].split("&");  
  52.     for (var i = 0; i < params.length; i = i + 1) {  
  53.         var singleParam = params[i].split("=");  
  54.         if (singleParam[0] == paramToRetrieve) return singleParam[1];  
  55.     }  

Publish

Publish the App and click the Trust it button.



Output

The folder has been created successfully in the Document Library SharePointLibraray.