Deleting a List on the Host Web from the App Web with JavaScript Object Model (JSOM)

Below is some sample code of how to delete a list on the host web from the app web via SharePoint JavaScript object model
function DeleteList(listName) {

    //Using the App Web as the client context
    clientContext = new SP.ClientContext.get_current();

    //Get the host web URL from the query string params
    //I have a function getHostWebUrl() - which is not included.
    var hostWebUrl = getHostWebUrl();
    //Using the hostWebContext as an AppContextSite
    hostWebContext = new SP.AppContextSite(clientContext, hostWebUrl);

    //get the list using the host web context
    var list = hostWebContext.get_web().get_lists().getByTitle(listName);

    //Always use the context of the app web to do the work or load and executing
    clientContext.executeQueryAsync(function() {
        console.log("Deleted List : \"" + listName + "\"");
    }, function(sender, args) {
        console.log("Failed to delete list : " + listName);
        console.log("Reason : " + args.get_message() + "");


// parameters : List Name
DeleteList("My Test List");

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.