Delete a List in SharePoint 2013 Using CSOM-JavaScript

Introduction

This article explains how to delete a list in SharePoint 2013 using CSOM-JavaScript.

Prerequisites

  1. Ensure you have access to the Office 365 online.
  2. Ensure Napa tool is available in your site.

Use the following procedure:

  1. 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.
  2. Click on Default.aspx page.

    aspx page

  3. Add a button to delete the list and place it inside the “PlaceHolderMain” tag.
  4. 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.
     

  5. Click on the "App.js" file.

    js file

  6. 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;
     

  7. Now write the function to delete the list in the site.
     

        function deleteList() {

            targetList = list.getByTitle("MyCustomList");

            targetList.deleteObject();

            context.executeQueryAsync(DeleteListSuccess, DeleteListFail);

        }
     

  8. “deleteObject” is the method that is used to delete the list from the site.
  9. Call deleteObject for the list that you want to delete.
  10. 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());

        }
     

  11. Please find following the final execution of the code.

    code

  12. That is it! Now let's execute the code.
  13. Click here to check how to get the listnames in the current site (getListName() method).

Testing

  1. Now to run the app click on the "Play" button that is available towards the left-most corner.

    Play button

  2. The app is packaged, deployed, and installed on your Office 365 Site.

    app

  3. Now you will be able to see the page where the current site collection lists are displayed.

    current site collection

  4. Now let's delete the list “MyCustomList” by clicking on the “Delete list” button.

    MyCustomList

Summary

Thus in this article you saw how to delete a list in SharePoint 2013 using CSOM-JavaScript.

Up Next
    Ebook Download
    View all
    Learn
    View all