Removing Folder In A SharePoint Document Library Using JavaScript Object Model (JSOM)

Introduction

In this article, we will learn how to remove a folder in Sharepoint document library, using SharePoint-hosted SharePoint add-ins, using Napa Office 365 development tools. Sharepoint's basic operations are used by JavaScript Object Model here. If there are any changes, post your comments here.

Develop the project, using the method given below in Napa tool

  • On your developer site, open 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 Create button.
  • Replace APP.js with the source code given below.
  • Publish your app.

Prerequisite

  • The important steps are given below, which are to be followed before creating the app.
  • Specify the permissions that your app needs, as shown below.
  • Choose the Properties button at the bottom of the page.
  • In the Properties Window, choose Permissions.
  • In the Content category, set the Write permissions for the Tenant scope.
  • In the Social category, set the Read permissions for the User Profiles scope.
  • Close the Properties Window.

Basic and important things that we want to know are given below.

Sp.ClientContext

  • Represents SharePoint Objects and Operations context.
  • This is used to access SiteCollection, SubSite, List,Library etc.
  • This is used to perform async calls to SharePoint to access the data.
  • Load method: Retrieves the properties of a client object from the Server.
  • executeQueryAsync method: Executes the current pending request asynchronously on the Server.

Reference the required libraries

Before using JSOM, you must reference the libraries in the order given below.

  1. ASP.NET AJAX library
  2. sp.runtime.js file
  3. sp.js file

Creating, updating and deleting the lists through Client Object Model works similarly to Server Object Model, using .NET Framework (CSOM, SSOM). .

The operations do not complete until and unless, you call the executeQueryAsync(succeededCallback, failedCallback) function.

SharePoint 2013 introduces a Napa tool Service, which is comparable to the existing SharePoint Client Object Model.

Napa tool can be used to perform Create, Read, Update and Delete (CRUD) operations from their apps for SharePoint.

By following this article, you can learn how to create a simple SharePoint-hosted SharePoint add-in by using Napa. The add-in that you’ll create includes the controls and the code to manage the lists and list items.

Choose the kind of add-in; you want to create, name the project and then choose the Create button.

Common steps for all Napa tool deployment


Napa is a tool, which you can use to create SharePoint-hosted SharePoint add-ins.

We can't create a PHA (Provider Hosted APP), using Napa tool.

Click Create button.


Default .aspx display is like the pic, shown above and App.Js file looks, as shown.

You can add more .js file and add the code to it instead of it to the existing file.


Now, publish your app.


Choose the Click here to launch your add-in in a new Window link.


Click Trust it option to deploy your app here, followed by your login popup Window, which will come and ask your credentials.

Enter your credentials and then click OK.




In the App.js, the variables are declared to store the Host Web and App Web.

  1. var hostWebUrl;  
  2. var appWebUrl;  
Get the Host Web and App Web URL in document.ready.
  1. hostWebUrl= decodeURIComponent(manageQueryStringParameter('SPHostUrl'));  
  2. appWebUrl = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));  
In the code given above, SPHostUrl represents the full URL of the host site and SPAppWebUrl represents the full URL of the app Web.
  1. <script type="text/javascript">  
  2.     var hostweburl;  
  3.   
  4.     // Load the required SharePoint libraries.  
  5.     $(document).ready(function () {  
  6.   
  7.         // Get the URI decoded URLs.  
  8.         hostweburl =decodeURIComponent( getQueryStringParameter("SPHostUrl") );  
  9.   
  10.         // The js files are in a URL in the form:  
  11.         // web_url/_layouts/15/resource_file  
  12.         var scriptbase = hostweburl + "/_layouts/15/";  
  13.   
  14.         // Load the js files and continue to  
  15.              $.getScript(scriptbase + "SP.Runtime.js",  
  16.             function () {  
  17.                 $.getScript(scriptbase + "SP.js", createFolder);  
  18.             }  
  19.         );  
  20.     });  
  21.   
  22. function deleteFolder(resultpanel) {  
  23.     var clientContext;  
  24.     var oweb;  
  25.     var _folderUrl;  
  26.   
  27.     clientContext = new SP.ClientContext.get_current();  
  28.     oweb= clientContext.get_web();  
  29.   
  30.     clientContext.load(oweb);  
  31.     clientContext.executeQueryAsync(function () {  
  32.         _folderUrl = oweb.get_serverRelativeUrl() + "/Lists/TutorialLibrarury/Folder1";  
  33.         this.folder = oWebsite.getFolderByServerRelativeUrl(_folderUrl);  
  34.         this.folder.deleteObject();  
  35.   
  36.         clientContext.executeQueryAsync(  
  37.             Function.createDelegate(this, successHandler),  
  38.             Function.createDelegate(this, errorHandler)  
  39.         );  
  40.     }, errorHandler);  
  41.     function success() {  
  42.         alert(“New folder Deleted successfully”);  
  43.     }  
  44.   
  45.     function error(sender,args) {  
  46.     alert( "Request failed: " + arguments[1].get_message();  
  47.     }  
  48. }  
  49.   
  50.     // Function to retrieve a query string value.  
  51.     // For production purposes you may want to use  
  52.     // a library to handle the query string.  
  53.     function getQueryStringParameter(paramToRetrieve) {  
  54.         var params =  
  55.             document.URL.split("?")[1].split("&");  
  56.         var strParams = "";  
  57.         for (var i = 0; i < params.length; i = i + 1) {  
  58.             var singleParam = params[i].split("=");  
  59.             if (singleParam[0] == paramToRetrieve)  
  60.                 return singleParam[1];  
  61.         }  
  62.     }  
  63. <script>   

Once the code has been executed, go back to SharePoint site and check if the list operations have executed successfully or not.

Thanks for reading the article.