How To Delete Attachments Of SharePoint List Item Using Rest API

Introduction

In this article, we will explore how to delete the Sharepoint list item attachments in Sharepoint 2013, using REST API and jQuery.

To remove attachments, I am using REST API. To delete only specific attachments (based on the attached file name and list Item), we need to build a URL, as shown below

{SiteURL}/_api/web/lists/GetByTitle([ListTitle])/GetItemById([ItemID])/AttachmentFiles/getByFileName([ FileTitle ]) 

Scenario

I have created a custom list named “Attachments” on the host site and have added multiple items with attachments. Now, let’s say that we want to “Delete” the attachments on any particular item which we want.

Sharepoint

I have an item (Item ID: 1) that has the following attachments.

Sharepoint

Objective

I wanted to get the URLs of the list item attachments with delete option so that I could use it in my HTML. To fetch the Item ID of the list item and bind to the drop-down. Once we have selected any Item ID from the list of Item IDs from the drop-down, the attachments of the respective item are shown. Click on to Delete the Attachment on the item.

Use the procedure given below.

Step 1

Navigate to your SharePoint 2013 site.

Step 2

From this page, select Site Actions | Edit Page.

Edit the page, go to the Insert tab in the Ribbon and click Web Part option. In Web Parts picker area, go to the Media and Content category, select the Script Editor Web Part and press the Add button.

Step 3

Once the Web Part is inserted into the page, you will see an "EDIT SNIPPET" link; click it. You can insert HTML and/or JavaScript, as shown below.

  1. <script type="text/javascript" src="../../SiteAssets/Script/jquery-1.9.1.min.js"></script>  
  2.     <script type="text/javascript">  
  3.         $(document).ready(function ($) {  
  4.             var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Attachments')/items?$select=Id";  
  5.             getListItems(url, function (data) {  
  6.                 var items = data.d.results;  
  7.                 var SelectElement = '<select id="drpListItem" style="width:100%" name="options"><option  value="">Select</option>';  
  8.                 // Add all the Item Id in Dropdown  
  9.                 for (var i = 0; i < items.length; i++) {  
  10.                     var itemId = items[i].Id;  
  11.                     SelectElement += '<option value="' + itemId + '"selected>' + itemId + '</option>';  
  12.                 }  
  13.                 SelectElement += '</select>';  
  14.                 $('#ItemID').append(SelectElement);  
  15.   
  16.                 // assign the change event   
  17.                 $('#drpListItem').on('change'function () {  
  18.                     if ($(this).val() != "") {  
  19.                         var Requestorurl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Attachments')/items(" + $(this).val() + ")/AttachmentFiles";  
  20.                         var ID = $(this).val();  
  21.                         getListItems(Requestorurl, function (data) {  
  22.                             var results = data.d.results;  
  23.                             var htmlattach = "";  
  24.                             if (data.d.results.length > 0) {  
  25.                                 $.each(data.d.results, function () {  
  26.   
  27.                                     if (htmlattach === "") {  
  28.                                         htmlattach = "<p class='uploaded_image'>     <a href='javascript:void(0);' id='" + ID + "' class='remove'>×</a>     <span class='filename'><a id='attachment' href='" + this.ServerRelativeUrl + "'>" + this.FileName + "</a></span></p>";  
  29.                                
  30.                                     }  
  31.                                     else {  
  32.                                         htmlattach = htmlattach + "<p class='uploaded_image'>  <a href='javascript:void(0);' id='" + ID + "' class='remove'>×</a>     <span class='filename'><a id='attachment' href='" + this.ServerRelativeUrl + "'>" + this.FileName + "</a></span></p>";  
  33.                                     }  
  34.   
  35.                                 });  
  36.   
  37.                             }  
  38.                             else { htmlattach = "<strong>There are no attachments to in this item.<strong>"; }  
  39.                             $('#attachmentsContainer').html(htmlattach);  
  40.                         });  
  41.                     }  
  42.                 });  
  43.   
  44.             }, function (data) {  
  45.                 console.log("An error occurred. Please try again.");  
  46.             });  
  47.   
  48.   
  49.             $(document).on("click""a.remove"function () {  
  50.                 DeleteItemAttachment($(this).attr('id'), $(this).next('span').text());  
  51.                 $(this).parent().remove();  
  52.             });  
  53.   
  54.   
  55.         });  
  56.         function getListItems(siteurl, success, failure) {  
  57.             $.ajax({  
  58.                 url: siteurl,  
  59.                 method: "GET",  
  60.                 headers: { "Accept""application/json; odata=verbose" },  
  61.                 success: function (data) {  
  62.                     success(data);  
  63.                 },  
  64.                 error: function (data) {  
  65.                     failure(data);  
  66.                 }  
  67.             });  
  68.         }  
  69.         function DeleteItemAttachment(ItemId, FileTitle) {  
  70.             var Dfd = $.Deferred();  
  71.             var Url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Attachments')/GetItemById(" + ItemId + ")/AttachmentFiles/getByFileName('" + FileTitle + "')  ";  
  72.             $.ajax({  
  73.                 url: Url,  
  74.                 type: 'DELETE',  
  75.                 contentType: 'application/json;odata=verbose',  
  76.                 headers: {  
  77.                     'X-RequestDigest': $('#__REQUESTDIGEST').val(),  
  78.                     'X-HTTP-Method''DELETE',  
  79.                     'Accept''application/json;odata=verbose'  
  80.                 },  
  81.                 success: function (data) {  
  82.                     Dfd.resolve(data);  
  83.                 },  
  84.                 error: function (error) {  
  85.                     Dfd.reject(JSON.stringify(error));  
  86.                 }  
  87.             });  
  88.             return Dfd.promise();  
  89.         }  
  90.   
  91.     </script>  

In the below screenshot, we have deleted “Account.doc” File on click delete Sharepoint sign.

Sharepoint

Final Output

Sharepoint
Sharepoint