Wednesday, April 1, 2015

In previous few articles we saw how to work with files and folders in the documents library using SharePoint 2013 REST services. Now lets see how to work with attachments of list items using SharePoint 2013 REST services. They are also important part of the SharePoint list. So SharePoint has given necessary APIs to interact with attachments. In this article we will see how to get attachments of list item.

Following is the REST URL to get all attachments:

/_api/web/lists/getbytitle([List Title])/items(1)/AttachmentFiles/

In response you will get all details of file as well as the URL of file.

Following is the sample code snippet to get all attachments:

<meta content="IE=edge" http-equiv="X-UA-Compatible">
<script src="/Site%20Assets/js/jquery-1.11.1.min.js"></script>
function GetAllAttachments()
                url: "/_api/web/lists/getbytitle('testlist')/items(1)/AttachmentFiles/",  
                method: "GET",
                headers: {
                                "ACCEPT": "application/json;odata=verbose",
                                "content-type": "application/json;odata=verbose"                         
                success: function (data) {
                                var htmlStr = "";
                                $.each(data.d.results, function(){
                                                if(htmlStr === "")
                                                                htmlStr = "<li 'ServerRelativeUrl="+ this.ServerRelativeUrl + "'>"  + this.FileName + "</li>";
                                                                htmlStr = htmlStr + "<li 'ServerRelativeUrl="+ this.ServerRelativeUrl + "'>"  + this.FileName + "</li>";
                error: function (data) {
                                alert("Error occured." + data);


                <input type="submit" onclick="GetAllAttachments()" value="Get All Attachments"> </input>

                <ul id="foldersDiv">

Happy SharePoint coding.


Post a Comment