Introduction
This article explains how to delete a folder in a Document Library in SharePoint 2013 using CSOM-JavaScript.
Prerequisites
- Ensure you have access to the Office 365 online.
- Ensure the 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.
- Create a Docment Library and name it “MyDocumentLibrary”. Refer to my article if you want to understand how to create Document Library in SP2013. Click Here.
- Create a folder and name it “MyCustomFolder”. If you want to understand how to create a folder then Click here.
- Click on the Default.aspx page.
- Add a button to delete a folder and place it inside the “PlaceHolderMain” tag as in the following:
<button id="btnDeleteFolder" onclick="deleteFolder()">Delete Folder</button><br/>
- Click on the App.js file.
- Globally declare the content and web objects as shown below.
var context = SP.ClientContext.get_current(); //gets the current context
var web = context.get_web(); //gets the web object
var folderUrl;
var folderToDelete;
- Now write the function to delete a folder in a Document Library as in the following:
function deleteFolder() {
context.load(web);
context.executeQueryAsync(getServerRelativeURL, onFolderDeleteFail);
}
function getServerRelativeURL() {
folderUrl = web.get_serverRelativeUrl() + "/MyDocumentLibrary/MyCustomFolder";
//specify the Document Library name and the folder name that you want to delete.
folderToDelete = web.getFolderByServerRelativeUrl(folderUrl);
folderToDelete.deleteObject();
context.executeQueryAsync(onFolderDeleteSuccess, onFolderDeleteFail);
}
- Here in this example we are deleting an exsisting folder.
- To delete the folder we are using the “getFolderByServerRelativeUrl” method to retrieve the folder from the Document Library. Execute the code by calling executeQueryAsync () to get the relative URL.
- And then we are calling the “deleteObject” method on the item object.
- Now execute the code by calling executeQueryAsync ().
function onFolderDeleteSuccess() {
alert("Folder Deleted");
}
// This function is executed if the above call fails
function onFolderDeleteFail(sender, args) {
alert('Failed to delete the Folder. Error:' + args.get_message());
}
- That's it! Now let's start testing.
Testing
- Now to run the app click on the "Play" button 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.
- Before clicking on the Delete Folder let's navigate to the following URL.
Syntax: https://yoursite/DocumentLibraryName
Example:https://mysite/MyDocumentLibrary
- Now you will be able to see the “MyCustomFolder”. Let's now proceed and click on the “Delete Folder” button.
- Navigate to the Document Library. Magic! The “MyCustomFolder” has been deleted from the Document Library.
Summary
Thus in this article you saw how to delete a folder in a Document Library in SharePoint 2013 using CSOM-JavaScript.