Delete a File in SharePoint 2013 Using JavaScript Object Model (JSOM)

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 shows the operations.

To delete a file, call the deleteObject() function on the object. The following example uses the getFileByServerRelativeUrl method to retrieve the file from the Document Library and then deletes the item.

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


Figure 1: JavaScript File

After adding a JavaScript file, this file will be available in the SharePoint Site Assets Folder.


Figure 2: Site Assets Folder

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


Figure 3: Sp Site Page


Figure 4: New Page

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


Figure 5: Ribbon Menu

Then add a Content Editor Web part into the page.


Figure 6: Content Editor

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


Figure 7: Edit the web part

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


Figure 8: Save the web Part


Figure 9: List deleted successfully

List deleted Successfully.

Source Code
  1. < div > < button onclick = " deleteFile ()" > Click here to deleteFile < /button></div >  
  2.   
  3. < div id = " resultpanel " > < /div>  
  4. <script type="text/javascript  
  5. ">   
  6. function deleteFile(resultpanel)   
  7. {  
  8. var clientContext;  
  9. var oWebsite;  
  10. var fileUrl;  
  11. clientContext = new SP.ClientContext.get_current();  
  12. oWebsite = clientContext.get_web();  
  13. clientContext.load(oWebsite);  
  14. clientContext.executeQueryAsync(function ()   
  15. {  
  16. fileUrl = oWebsite.get_serverRelativeUrl() +  
  17. " / Lists / Shared Documents / TextFile1.txt ";  
  18. this.fileToDelete = oWebsite.getFileByServerRelativeUrl(fileUrl);  
  19. this.fileToDelete.deleteObject();  
  20.   
  21. clientContext.executeQueryAsync(  
  22. Function.createDelegate(this, successHandler),  
  23. Function.createDelegate(this, errorHandler)  
  24. );  
  25. }, errorHandler);  
  26.   
  27. function successHandler()   
  28. {  
  29. resultpanel.innerHTML =  
  30. "  
  31. Go to the " +  
  32. " < a href = '../Lists/Shared Documents' > document library < /a> " +  
  33. "to confirm that the \"TextFile1.txt\" file has been deleted.";  
  34. }  
  35.   
  36. function errorHandler() {  
  37. resultpanel.innerHTML = "Request failed: " + arguments[1].get_message();  
  38. }  
  39. }  
  40.   
  41. </script >
Thanks for reading the article.

Up Next
    Ebook Download
    View all
    Learn
    View all