Delete Folder in Document Library Using REST

This article explains how to delete a folder in a Document Library using REST. Develop the project using the following Method in 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:These 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 default aspx page in app.

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

URI:



Lib=SharePoint Library Name
Folder B=New Folder 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.     //Get the URI decoded URLs.  
  7.     hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));  
  8.     appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));  
  9.     // Resources are in URLs in the form:  
  10.     // web_url/_layouts/15/resource     
  11.          $("#btnclick").click(function (event)
            {  
  12.             FolderCreation();  
  13.             event.preventDefault();  
  14.         });  
  15.     // Load the js file and continue to load the page with information about the folders.  
  16.     // SP.RequestExecutor.js to make cross-domain requests  
  17.     $.getScript(hostweburl + "/_layouts/15/SP.RequestExecutor.js");  
  18. });  
  19. //Retrieve all of the folders from root Site  
  20. function FolderCreation()  
  21. {  
  22.     var executor;  
  23.  var getfoldername= document.getElementById("DeleteFolder").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/Folder B')?@target='" + hostweburl + "'",  
  28.         method: "POST",  
  29. }         
  30.           headers:   
  31.             {  
  32.               "X-HTTP-Method":"DELETE"   
  33.             },    
  34. success: FoldersSuccessHandler,  
  35.         error: FoldersErrorHandler  
  36.     });  
  37. }   
  38. //Populate the selectFolders control after retrieving all of the folders.  
  39. function FoldersSuccessHandler(data) {  
  40.     alert("Folder Deleted successfully in Library");  
  41. }   
  42. function FoldersErrorHandler(data, errorCode, errorMessage) {  
  43.     alert("Could not Delete a Folder in  Library: " + errorMessage);  
  44. }  
  45. //Utilities  
  46. // Retrieve a query string value.  
  47. // For production purposes you may want to use a library to handle the query string.  
  48. function getQueryStringParameter(paramToRetrieve) {  
  49.     var params = document.URL.split("?")[1].split("&");  
  50.     for (var i = 0; i < params.length; i = i + 1) {  
  51.         var singleParam = params[i].split("=");  
  52.         if (singleParam[0] == paramToRetrieve) return singleParam[1];  
  53.     }  

Publish:

Publish the App and Click the Trust it Button.


Output:

Folder Deleted Successfully in Document Library SharePointLibraray,

Up Next
    Ebook Download
    View all
    Learn
    View all