In this blog,  we will explore how to show the SharePoint list item level attachments using REST API and jQuery.

In a previous article, we explained about basic operations using REST API .

Now, let’s use some REST API to pull these attachments and display them in the list.

Konnectogrow will provide you the best digital marketing services in Pune and Noida, we also provide web development services, app development services, and graphic designing services in Pune you can grab the services to increase your business. Microsoft SharePoint designer 2013 training is available at Konnectogrow Pune, we can give you the best SharePoint developer training with the handling of the live project. As well as we also provide Sharepoint services and the best SharePoint consulting services in Pune. Our SharePoint designer has uploaded the SharePoint image to make you understand what is SharePoint? You can grab all services from Konnectogrow Pune.

To bind the attachment to the control you have to first create the attachment as below

<tr><td id=”tdfp”><div id=”attachment” style=”width:100%;”>
<input type=”file” id=”fp1″ style=”width:98%;”>
<label id=”lblattachmentvalue” style=”display:none;”></label></div></td></tr>

To get Item level attachment you have to pass the itemid(ID) into function.

Now question is that how to get item id so you can get various way now i am assuming we are getting from url with help of below fucntion

getParameterByName(“ID”)

where ID is parameter which you are passing into query string as parameter(It’s name not value of the parameter)

<script src=”/Style Library/js/jquery-1.12.4.min.js”>

$(document).ready(function () {

SP.SOD.executeFunc(‘sp.js’); SP.SOD.executeFunc(‘sp.ui.dialog.js’);

var rID = getParameterByName(“ReqID”);

if (rID.length > 0) {

GetAttachment (rID);}

});

function getParameterByName(name) {

name = name.replace(/[\[]/, “\\[“).replace(/[\]]/, “\\]”);

var regex = new RegExp(“[\\?&]” + name + “=([^&#]*)”),

results = regex.exec(location.search);

return results === null ? “” : decodeURIComponent(results[1].replace(/\+/g, ” “));

}

function GetAttachment(reqid) {

var queryUrl = _spPageContextInfo.webAbsoluteUrl + “/_api/web/lists/getbytitle(‘yourListName’)/getitembyid(” + reqid + “)/AttachmentFiles”;

$.ajax({

url: queryUrl,

type: “get”,

headers: { “Accept”: “application/json;odata=verbose” },

success: function (data) {

var count = 0, var attachmentname = “”; var attachmenturl = “”, var url = “”;

$.each(data.d.results, function (i, result) {

count++; attachmentname = result.FileName;

attachmenturl = result.ServerRelativeUrl;

url = attachmenturl + “/” + attachmentname;

var mydiv = document.getElementById(“attachment”);

var aTag = document.createElement(‘a’);

aTag.setAttribute(‘href’, attachmenturl);

aTag.innerHTML = attachmentname + “</br>”;

aTag.target = “_blank”;

mydiv.appendChild(aTag);

})

if (count > 0) {

$(“#fp1”).hide();

}

else {

$(“#fp1”).hide();

$(“#lblattachmentvalue”).show();

document.getElementById(‘lblattachmentvalue’).innerText = “No attachments Found !”;

} },

error: function (data) {

alert(data.responseJSON.error);   });  }</script>

where _spPageContextInfo.webAbsoluteUrl is your site url where your list exist.

Konnectogrow will provide you the best digital marketing services in Pune and Noida, we also provide web development services, app development services, and graphic designing services in Pune you can grab the services to increase your business. Microsoft SharePoint designer 2013 training is available at Konnectogrow Pune, we can give you the best SharePoint developer training with the handling of the live project. As well as we also provide Sharepoint services and the best SharePoint consulting services in Pune. Our SharePoint designer has uploaded the SharePoint image to make you understand what is SharePoint? You can grab all services from Konnectogrow Pune.