6

I use REST API SharePoint 2013 to get the items from list: And and I want to get the Attachments Files, How can i do that?

This the code :

function getListItems(url, listname, query, complete, failure) {

    // Executing our items via an ajax request
    $.ajax({
        url: url + "/_api/web/lists/getbytitle('" + listname + "')/items" + query,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            complete(data); // Returns JSON collection of the results

        },
        error: function (data) {
            //failure(data);
            alert("failure");
        }
    });

}

I use the console of my navigator : data.d.results[5].Attachments=true

data.d.results[0].AttachmentFiles.__deferred.uri= http://wbcjdyzd/_api/Web/Lists(guid'ba46572a-3bac-4813-8692-d890fbb88b29')/Items(14)/AttachmentFiles

But I don't get the URL of the file.

7

You need to $select and $expand the AttachmentFiles property for your list items if you want to select many items from the list.

_api/lists/getByTitle('MyList')/items?$select=AttachmentFiles,Title&$expand=AttachmentFiles

Then you can get attachment info (if it exists) in the AttachmentFiles property of your list item which will be an array of AttachmentFile objects.

I suggest you just check out the return values in your dev tools to inspect the structure with something like:

$.getJSON("/_api/lists/getByTitle('MyList')/items?$select=AttachmentFiles,Title&$expand=AttachmentFiles", 
    function(data) { console.log(data) })

But you can do something like retrieve the file's URL with:

data.value[0].AttachmentFiles[0].ServerRelativeUrl

If your list item doesn't have any attachments, the AttachmentFiles will be an empty array.

I can't seem to get the $filter operator to work with the Attachments property to filter the results for only those items with attachments -- maybe someone else out there has some experience getting that up and running and would like to add it here =)

  • @Jens made a comment that using $select and $expand doesn't seem to work in a 2013 On-premises environment: I'm beginning to think that the REST API has been patched up by some of the updates Microsoft has been rolling out. This works as advertised for me using SharePoint online as of the time of my answer. Is your on-prem environment up to date with all the CUs? – John-M Jan 15 '15 at 13:14
  • What about adding an ID for a specific list item and include the attachment? – Si8 Jul 25 '17 at 17:52
  • You need to add Attachment in $select, without this it returns error – Ankitkumar Malde Mar 26 '18 at 11:08
  • Filtering for items with attachments is accomplished via $filter=Attachments eq 1. – Jinxed Apr 6 '18 at 7:07
5

Not enough Reputation to add a comment, but DacrioS has cracked it. I have an on-premises install of Sharepoint 2013 and could not get attachments to show up when using _api/web REST interface. Until I read this comment: "Try adding Attachments to the select."

Let me be clear:

This Does Not Work _api/web/lists/getbytitle('MYLIST')/items?$filter=Id%20eq%202451&$select=AttachmentFiles&$expand=AttachmentFiles

This DOES WORK _api/web/lists/getbytitle('MYLIST')/items?$filter=Id%20eq%202451&$select=Attachments,AttachmentFiles&$expand=AttachmentFiles

Notice the ONLY difference between the two queries is the inclusion of Attachments in the $select statement. Add that and booya - I get Attachment files.

3

If you get a message like: The 'Attachments' column doesn't exists.

Try adding 'Attachments' to the select, like: _api/lists/getByTitle('MyList')/items?$select=Attachment,AttachmentFiles,Title&$expand=AttachmentFiles

  • This does not seem to be an answer to posted question but one of the answers. If you want to comment to author of a question/answer then first attain 50 reputations and get eligible for commenting everywhere. – Asad Refai Jul 23 '15 at 11:47
2

You can get attachments using

var url = url + "/_api/web/lists/getbytitle('ListTitle')/items(1)/AttachmentFiles";

$.ajax({
    url: url,
    accepts: {
        json: "application/json;odata=verbose"
    },
    method: "GET",
    success: onQuerySuccess,
    error: onQueryError
});

function onQuerySuccess(data) {
    if (data) {
        $.each(data.d.results, function () {
            // do something
            this.ServerRelativeUrl;
        });
    }
}
1

I don't have enough reputation to comment so I can only "answer" :-( The answer from john-m sounds great but it does not seem to work?

$select=AttachmentFiles,Title does return the expected result, AttachmentFiles as deferred url

just $expand=AttachmentFiles without any $select= does work, gets all fields and an array of attachments

But $select=AttachmentFiles,Title&$expand=AttachmentFiles returns an error that "value": "Value does not fall within the expected range."

Looks like here, SP2013 on premise, only select OR expand but not combined :-(

john-m please, does select + expand this actually work for you?

  • I added a comment to my own answer in case this gets removed -- is your on-prem environment updated with the CUs by any chance? My answer works as advertised using SP Online – John-M Jan 15 '15 at 13:15
  • Working on a new, similar project I learned that using the older/other REST endpoint "_vti_bin/listdata.svc" actually does allow to select, expand, filter better than the new "_api/web/" stuff – Jens Feb 3 '16 at 15:18
0

If you want to get the attachments for a specific item from the custom list, you need to have the internal ID of the record item.

Once you have your ID, you can do a rest call, here the rest URL:

/_api/lists/getByTitle('{{ListName}}')/items({{ID}})/AttachmentFiles

You just need to pass the List name and the item ID.

BR, jasminb

0

//you can re-iterate inside the success to call the Attachments api
//for every item inside the data result of the first out ajax call

 $.ajax({
        url: Siteurl + "/_api/web/lists/getbytitle('" + listname + "')/items" + query,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            complete(data); // Returns JSON collection of the results
            [].forEach.call(data.d.results,function(value,index){
                 var attachmentsUrl = Siteurl + "/_api/web/lists/getbytitle('"+listname+"')/items("+value.ID+")/AttachmentFiles";
                 $.ajax({
                     url:attachmentsUrl,
                     method:"GET",
                     headers: { "Accept": "application/json; odata=verbose" },
                     success:function(dataInner){
                        value.Attachments = dataInner.d.results;
                     }
                 });
            });
        },
        error: function (data) {
            //failure(data);
            alert("failure");
        }
    });

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.