7

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.

8

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 =)

8
  • @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 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
  • @John-M Getting "Column 'Attachments' does not exist. It may have been deleted by another user." Using this Jan 19 '20 at 20:31
10

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.

2
  • getting "The field or property 'Attachments' does not exist." Jan 19 '20 at 20:32
  • @MayureshSrivastava The only way you get an error like that is if the list is corrupt, really missing the column, or you're using a document library. When you get things like that, try creating a brand new list & use it against a test item you create there by pasting the URL into a browser, with everything filled in as you think it should be. Also, you can't use the URL he did because he added a filter for an ID that probably doesn't exist on your list. Take it off: https://yoursite/sites/SC/_api/web/lists/getbytitle('MYLIST')/items?$select=Attachments,AttachmentFiles&$expand=AttachmentFiles
    – vapcguy
    Oct 1 at 20:11
4

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
  • Getting "Column 'Attachments' does not exist. It may have been deleted by another user." Using this Jan 19 '20 at 20:29
4

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

3
  • 1
    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
  • @Severino, getting "The field or property 'Attachments' does not exist." Jan 19 '20 at 20:34
  • The select statement has "Attachment", not "Attachments". It should be "Attachments".
    – vapcguy
    Oct 1 at 20:19
3

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?

3
  • 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
  • I voted this up because I thought you might've been on to something, but when I tried it, myself, I have no issues with a select on both Attachments and Attachment Files, with an expand on AttachmentFiles.
    – vapcguy
    Oct 1 at 20:37
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");
        }
    });
0

You need to have Attachments so you can test if you have one, and then get and expand AttachmentFiles:

 var query = "$select=Title,Attachments,AttachmentFiles&$expand=AttachmentFiles";

Once you have that in your query string, you combine that with your site's URL where the list is (chop off "/Lists/MyList" and everything after it), with /_api/web/lists/getbytitle("YourListNameHere")/items. Your items are in the form of data.d.results, so you can loop through them. I assign that to a variable called items, and so I get items[0], items[1], items[2]... items[n].

Off of each items[i] you can check items[i].Attachments if it is true. If it is, you can get the link from items[i].AttachmentFiles.results[0].ServerRelativeUrl:

 var siteUrl = "https://mysite/sites/mysitecollection";
 var query = "$select=Title,Attachments,AttachmentFiles&$expand=AttachmentFiles";
 var picLink = "";

 $.ajax({
    url: siteUrl + "/_api/web/lists/getbytitle('" + listname + "')/items" + query,
    method: "GET",
    headers: { "Accept": "application/json; odata=verbose" },
    success: function (data) {
        var items = data.d.results;
        if (items.length > 0) {
            for (var i = 0; i < items.length; i++) {
                if (items[i].Title != undefined) {
                    if (items[i].Attachments) {
                        picLink = items[i].AttachmentFiles.results[0].ServerRelativeUrl;
                    }
                }
            } // end loop
        } // end if
    },
    error: function (data) {
        //failure(data);
        alert("failure");
    }
});
-1

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

2
  • Getting "Column 'Attachments' does not exist. It may have been deleted by another user." Using this Jan 19 '20 at 20:26
  • You have to expand AttachmentFiles, and this doesn't say how you get the resulting URL of the attachment. You also don't need a specific ID, you can loop through your list items, anyway.
    – vapcguy
    Oct 1 at 22:26

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.