Get All Attachments From SharePoint List Item using Rest API and JavaScript Object Model (JSOM)

This SharePoint Rest API tutorial we will discuss how we can retrieve all attachments from a SharePoint 2013 list item using Rest API in SharePoint 2013. Here I have a list and each item has some attachments. We will try to retrieve all the attachments from a particular item using Rest API in SharePoint 2013/Online.

If you are new to Rest API, then check SharePoint Rest API Tutorial and Examples.

Get All Attachments From SharePoint 2013 List Item using Rest API

In this particular example, I am trying to retrieve attachments from list item whose id is 3.

Put the below code in a script editor web part or inside a content editor web part.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(function(){
$("#btnClick").click(function(){
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle(‘MyTestList’)/items(3)/AttachmentFiles";
$.ajax({
url: requestUri,
type: "GET",
headers: {
"accept":"application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest":$("#_REQUESTDIGEST").val()
},
success: onSuccess,
error: onError
});

function onSuccess(data) {
var itemsCount = data.d.results.length;
alert (itemsCount);
if (data) {
$.each(data.d.results, function () {
// do something
alert( this.ServerRelativeUrl);
});
}
}

function onError(error) {
alert(JSON.stringify(error));
}
});

});

</script>

<input type="button" id="btnClick" value="Get Attachments"/>

Once you save the code and click on the button, it will display all the attachments in the alert dialog box in SharePoint Online or SharePoint 2013/2016.

Get All Attachments From SharePoint List Item using JavaScript Object Model (JSOM)

Now, we will discuss, how to get list item attachments using the JavaScript client object model (jsom) in SharePoint 2013/2016/Online.

Whenever a user does an attachment then the attachments are stored in an attachment folder and you can view that items attachment by URL:

{SITE URL}/Lists/{LIST NAME}/Attachments/{ITEM ID}/{ATTACHED FILE}

Example: http://<servername>/lists/MyTestList/Attachments/1/MyTestDoc.docx

Below is the JavaScript code to get all attachments from the SharePoint list item using JavaScript.

<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(getWebProperties, "SP.js");
var attachmentFiles;
function getWebProperties() {
var itemId=2;
var ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
var attachmentFolder=web.getFolderByServerRelativeUrl(‘Lists/City/Attachments/’+itemId);

attachmentFiles= attachmentFolder.get_files();
ctx.load(attachmentFiles);
ctx.executeQueryAsync(Function.createDelegate(this,this.onSuccess),Function.createDelegate(this,this.onFailed));
}
function onSuccess(sender, args) {
var i=0;
for(var file in attachmentFiles)
{
alert(attachmentFiles.itemAt(i).get_serverRelativeUrl());
i++;
}
}
function onFailed(sender, args) {
alert("Something went Wrong");
}
</script>

Here, we discussed how to get all list item attachments using jsom (JavaScript object model) in SharePoint.

You may also like following SharePoint Rest API tutorials:

Hope this SharePoint tutorial helps to get all attachments from a SharePoint list item using Rest API.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>