Introduction
This article explains how to delete a list 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 Default.aspx page.
- Add a button to delete the list and place it inside the “PlaceHolderMain” tag.
- The “listNames” div tag is used to display the lists available in the current site.
<button id="btnDeleteList" name="btnDeleteList" onclick="deleteList()">
Delete List
</button>
<div id="listNames"></div>
// listNames div tag is used to display the lists available in the current site.
- Click on the "App.js" file.
- Globally declare the content, web and list objects as shown below.
var context = SP.ClientContext.get_current(); //gets the current site
var web = context.get_web(); // gets the web object
var list = web.get_lists(); //gets the list collection
var targetList;
- Now write the function to delete the list in the site.
function deleteList() {
targetList = list.getByTitle("MyCustomList");
targetList.deleteObject();
context.executeQueryAsync(DeleteListSuccess, DeleteListFail);
}
- “deleteObject” is the method that is used to delete the list from the site.
- Call deleteObject for the list that you want to delete.
- Then execute the code by calling the executeQueryAsync () method.
function DeleteListSuccess() {
alert("List got deleted");
getListName();
}
function DeleteListFail(sender, args) {
alert('Failed to get the List. Error:' + args.get_message());
}
- Please find following the final execution of the code.
- That is it! Now let's execute the code.
- Click here to check how to get the listnames in the current site (getListName() method).
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 page where the current site collection lists are displayed.
- Now let's delete the list “MyCustomList” by clicking on the “Delete list” button.
Summary
Thus in this article you saw how to delete a list in SharePoint 2013 using CSOM-JavaScript.