Delete a List in SharePoint 2013 using JavaScript

In this article I would like to explain how to write code to do basic operations using the JavaScript client object model in SharePoint 2013.

You can use the SharePoint client object model to retrieve, update and manage data in SharePoint 2013. SharePoint makes the object model available in several forms.

  • .NET Framework redistributable assemblies.
  • JavaScript library.
  • REST/OData endpoints.
  • Windows Phone assemblies.
  • Silverlight redistributable assemblies.

This article shows how to do basic operations using the JavaScript object model. You can add a reference to the object model using HTML <script> tags.

The following sections describe tasks that you can complete programmatically and they include JavaScript code examples that demonstrate the operations.

Procedure

Open your SP Site in SharePoint 2013 Designer. Then select an Assets icon in the designer ribbon then add a JavaScript file.


After adding the JavaScript file, the file will be available in the SharePoint Site Assets Folder as in the following:



Go to the SP site page and add a new page to the SharePoint site.



After adding a page select an Insert button in the Ribbon Menu.



Then add a Content Editor Web part into the page.



Edit the WebPart and add a JavaScript file link to the contents link.



Save the web part and save the page in site. Click the button.


List Created Successfully.

Source Code

  1. <div><button onclick=" deleteList()">Click here to AddaFieldtoList</button></div>  
  2.   
  3. <div id="displayDiv"></div>  
  4. <script type="text/javascript">   
  5.   
  6.   
  7. //This function loads the list and runs the query asynchronously   
  8.   
  9. function deleteList() {  
  10. var siteUrl=”https://gauti.sharepoint.com/sites/sp/”;  
  11. var clientContext = new SP.ClientContext(siteUrl);  
  12. var oWebsite = clientContext.get_web();  
  13. this.listTitle = 'My Announcements List';  
  14.   
  15. this.oList = oWebsite.get_lists().getByTitle(listTitle);  
  16. oList.deleteObject();  
  17.   
  18. clientContext.executeQueryAsync(  
  19. Function.createDelegate(this, this.onQuerySucceeded),   
  20. Function.createDelegate(this, this.onQueryFailed)  
  21. );  
  22. }  
  23.   
  24. function onQuerySucceeded() {  
  25. var result = listTitle + ' deleted.';  
  26. alert(result);  
  27. }  
  28.   
  29. function onQueryFailed(sender, args) {  
  30. alert('Request failed. ' + args.get_message() +   
  31. '\n' + args.get_stackTrace());  
  32. </script>  
Thanks for reading my article.

Up Next
    Ebook Download
    View all
    Learn
    View all