4

PFB the code:

$.ajax({
        url: _spPageContextInfo.webAbsoluteUrl+"/_api/web/lists/GetByTitle('List Name')/items",
        type: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
        },
        success: function (data) {

            $scope.result=data.d.results;

        },
        error: function (error) {
            alert(JSON.stringify(error));
        }

});

I am able to retrieve the list data with above code, now I need to retrieve attachments along with the list data.

Thanks in advance

2

You can use OData syntax in the REST API to expand the attachments and return the ServerRelativeUrl:

/_api/web/lists/GetByTitle('List Name')/items?$select=AttachmentFiles,Title,AttachmentFiles/ServerRelativeUrl&$expand=AttachmentFiles

Check Use OData query operations in SharePoint REST requests for more information.

1

Maybe this will help (the whole script):

https://afrait.com/blog/get-attachment-url-from-list-item-en

Attachment is an Object. So get the whole object like this:

var attachmentUrl = item.AttachmentFiles.results[0]

And then you could do this to get the file name:

alert(attachmentUrl.FileName);

or save it to a variable of your choice.

  • But how do I iterate the attachments in ng-repeat ? When i perform item.AttachmentFiles.results[0].FileName , it is picking up only the first attachment – Dheeraj Oct 23 '17 at 8:44
0

You can retrieve the attachment files using the below code

 var requestUri = "/_api/lists/getbytitle('<List_Name>')/items?$select=Title,ID,Attachments,AttachmentFiles&$expand=AttachmentFiles";
 var attachmentUrl;

                    $.ajax({
                        url: requestUri,
                        type: "GET",
                        headers: { "ACCEPT": "application/json;odata=verbose" },
                        success: function (data) {

                            $.each(data.d.results, function (obj, itm){
                               attachmentUrl = item.AttachmentFiles.results;                            

                            })
                        },
                        error: function () {

                        }                     
                    });

To perform ng-repeat you can do,

<h1 ng-repeat="x in attachmentUrl ">{{x.FileName}} {{x.ServerRelativeUrl}}</h1>
0

I am showing you a POC of this, hope it works for you:

enter image description here

HTML:

<div id='mydiv'>
  <table id='mytable'>
    <tr>
      <th>ID</th>
      <th>Title</th>
      <th>Attachment</th>
    </tr>
  </table>
</div>

Script:

$(document).ready(function(){
var listurl="/_api/web/lists/GetByTitle('test1')/items?select=AttachmentFiles,Title,AttachmentFiles/ServerRelativeUrl&$expand=AttachmentFiles";
getItems(listurl);
});
function getItems(url) {
    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + url,
        type: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
        },
        success: function (data) {
            for(i=0; i < data.d.results.length; i++){    //will get the list of items one by one till its length
                $("#mytable").append("<tr id='datatr" + i +"'>");    //'datatr'+i will create new rows for each item, you can work your way round
                $("#datatr" + i +"").append("<td>" + data.d.results[i].ID +"</td>");
                $("#datatr" + i +"").append("<td>" + data.d.results[i].Title +"</td>");
            if(data.d.results[i].AttachmentFiles.results.length > 0)
                $("#datatr" + i +"").append("<td><a href='" +_spPageContextInfo.webAbsoluteUrl + data.d.results[i].AttachmentFiles.results[0].ServerRelativeUrl +"'>" + data.d.results[i].AttachmentFiles.results[0].FileName + "</a></td>");    //if attachment is there it will print its hyperlink
            else $("#datatr" + i +"").append("<td>No attachment</td>");
            $("#mytable").append("</tr>");
                }
        },
        error: function (error) {
            alert(JSON.stringify(error));
        }
    });
}

Here _spPageContextInfo.webAbsoluteUrl + data.d.results[i].AttachmentFiles.results[0].ServerRelativeUrl will give you the complete url of the attachment file and data.d.results[i].AttachmentFiles.results[0].FileName will give you the file name.

  • @Dheeraj please mark correct if it works for you. Thanks 😊 – Mayank Srivastava Oct 24 '17 at 8:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.