Show SharePoint List Item Level Attachments Using REST API And jQuery

Introduction

In this article, we will explore in Sharepoint 2013, how to show the Sharepoint list item level attachments using REST API and jQuery.  In the previous article, I explained about adding multiple attachments to list item using HTML and jQuery. Now, let’s use some REST API to pull these attachments and display them in the list.

For retrieving attachments, I am using REST API. The URL for all attachment collections can be built like below.

{Site URL}/_api/web/lists/getbytitle([List Title])/items([item ID])/AttachmentFiles

Scenario

I have created a custom list on the host site named “Attachment”. Add multiple items with attachments and let’s say that we want to show the item level attachments in the item selection.

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 so that I could use it in my HTML and 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. An "on change" event is used to fetch and we show the related attachments.

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.                         getListItems(Requestorurl, function (data) {  
  21.                             var results = data.d.results;  
  22.                             var htmlStr = "";  
  23.                             if (data.d.results.length > 0) {  
  24.                                 $.each(data.d.results, function () {  
  25.                                     if (htmlStr === "") {  
  26.                                         htmlStr = "<li><a id='attachment' href='" + this.ServerRelativeUrl + "'>" + this.FileName + "</a></li>";  
  27.                                     }  
  28.                                     else {  
  29.                                         htmlStr = htmlStr + "<li><a id='attachment' href='" + this.ServerRelativeUrl + "'>" + this.FileName + "</a></li>";  
  30.                                     }  
  31.                                 });  
  32.                             }  
  33.                             else { htmlStr = "<strong>There are no attachments to show in this item.<strong>"; }  
  34.                             $('#attachmentsContainer').html(htmlStr);  
  35.                         });  
  36.                     }  
  37.                 });  
  38.   
  39.             }, function (data) {  
  40.                 console.log("An error occurred. Please try again.");  
  41.             });  
  42.   
  43.         });  
  44.         function getListItems(siteurl, success, failure) {  
  45.             $.ajax({  
  46.                 url: siteurl,  
  47.                 method: "GET",  
  48.                 headers: { "Accept""application/json; odata=verbose" },  
  49.                 success: function (data) {  
  50.                     success(data);  
  51.                 },  
  52.                 error: function (data) {  
  53.                     failure(data);  
  54.                 }  
  55.             });  
  56.         }  
  57.   
  58.     </script>  

Final Output

  1. List Item with attachments.

    SharePoint

  2. List item without attachments.

    SharePoint