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
 
   1: function DeleteList(listName) {

   2:  

   3:     //Using the App Web as the client context

   4:     clientContext = new SP.ClientContext.get_current();

   5:  

   6:     //Get the host web URL from the query string params

   7:     //I have a function getHostWebUrl() - which is not included.

   8:     //http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript

   9:     var hostWebUrl = getHostWebUrl();

  10:     //Using the hostWebContext as an AppContextSite

  11:     hostWebContext = new SP.AppContextSite(clientContext, hostWebUrl);

  12:  

  13:     //get the list using the host web context

  14:     var list = hostWebContext.get_web().get_lists().getByTitle(listName);

  15:     list.deleteObject();

  16:     

  17:     //Always use the context of the app web to do the work or load and executing

  18:     clientContext.executeQueryAsync(function() {

  19:         console.log("Deleted List : \"" + listName + "\"");

  20:     }, function(sender, args) {

  21:         console.log("Failed to delete list : " + listName);

  22:         console.log("<span style='color:red'>Reason : " + args.get_message() + "</span>");

  23:     });

  24:  

  25: }

  26:  

  27: // parameters : List Name

  28: DeleteList("My Test List");

Advertisements