↓ Skip to main content
Deleting a List on the Host Web from the App Web with JavaScript Object Model (JSOM)

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

·1 min
Tom Daly
Author
Tom Daly
Developer, Designer, Thinker, Problem Solver, 10x Microsoft MVP for M365, & Collaboration Director @ SoHo Dragon.

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.
    //http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
    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);
    list.deleteObject();

    //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");

Related