2

I am trying to retrieve items from a Custom List in SharePoint 2013 along with any files that have been attached to those list items in one REST API call.

So, if I do this:

/_api/web/lists/getbytitle('Connect_Contribution')/items?$filter=&$top=100&$expand=AttachmentFiles&$orderby=Created%20desc

I get a really nice response with one object per list item called AttachmentFiles that contains a results array that contains any document that has been uploaded to that list item (i.e. AttachmentFiles.results[0] = {FileName: "bla bla", ServerRelativeUrl: "blah blah"}). Beautiful.

But of course, I want to limit the things that come back from this and I want to expand some other fields, so I really want to do this (add a $select portion):

/_api/web/lists/getbytitle('Connect_Contribution')/items?$filter=&$top=100&$select=Id,AttachmentFiles&$expand=AttachmentFiles&$orderby=Created%20desc

I get the error:

Value does not fall within the expected range.

Playing with the select a bit I can get results without an error, but not the results I want (remove $select reference to AttachmentFiles):

/_api/web/lists/getbytitle('Connect_Contribution')/items?$filter=&$top=100&$select=Id&$expand=AttachmentFiles&$orderby=Created%20desc

This returns a result, but now that I've specified a $select=Id, I ONLY get Id's and no AttachmentFiles object.

So, does the AttachmentFiles object have a special name or something that I need in the SELECT statement?

0

EDIT -- RE: 2013 _api/ (my original answer pertained only to listdata.svc, which has been generally replaced by _api in 2013)

The following URL uses what you were looking for and seems to work just fine on O365

/getByTitle('<ListName>')/items?$filter=substringof('Item', Title)
    &$select=AttachmentFiles,Title&$expand=AttachmentFiles&$top=4&$orderby=Title"

Your second example URL also seems to work on my O365 site without error even though the filter criteria is empty -- your list does have attachments enabled?


The below applies to listdata.svc in _vti_bin for 2010 and 2013:

Use something like ...$select=...,Attachments,...&$expand=Attachments...

Then (if you're using JSON) you'll get an array of attachment items with each list item (attachment array may be empty) you can access with data.d.results[i].Attachments.results

Without knowing exactly how you want to use the attachment info -- you can use this array to construct the actual attachment URLs like:

"/Lists/<listName>/Attachments/" + data.d.results[i].Id + "/" + 
 data.d.results[i].Attachments.results[k].Name;
10
  • Thanks for the feedback. I'm afraid that your suggestion doesn't work - and yes, I'm getting JSON Results and doing stuff with them. When I put the following in:_api/web/lists/getbytitle('Connect_Contribution')/items?$select=Attachments&$expand=Attachments I get the following error: The field or property 'Attachments' does not exist. Also, Attachments is a True/False field. AttachmentFiles is the file collection array.
    – skrile
    Aug 7 '14 at 16:54
  • @skrile that's interesting -- I have something like this running in production on a 2010 server (using _vti_bin of course) and it calculates URLs as part of a view model to display them as links on a page. I checked my O365 site (using _api) and it seemed to work the same way this morning... I'll give it another look later though.
    – John-M
    Aug 7 '14 at 17:10
  • Progress: If I use the listdata.svc, I can get what I'm looking for. _vti_bin/listdata.svc/Connect_Contribution?$select=Attachments,CreatedBy/Name,CreatedBy/Account&$expand=Attachments,CreatedBy&$top=30 This of course isn't the best solution in that it is relying on the old way of doing things, but it does give me what I'm looking for.
    – skrile
    Aug 7 '14 at 17:43
  • So, this works: $.ajax({ url: "<YOUR SHAREPOINT COLLECTION HERE>" + "_vti_bin/listdata.svc/Connect_Contribution?" + "$select=Id,Attachments,Contributor/Name,Contributor/Account,ConnectId/Id,ConnectId/MeetingDate&" + "$expand=Attachments,Contributor,ConnectId&$top=50&$orderby=ConnectId/MeetingDate desc", type: "GET", async: true, contentType: "application/json;odata=verbose", headers: { "Accept": "application/json;odata=verbose" }, success: function (data) { //if there is data if (data.d.results) { //do something with data }}});
    – skrile
    Aug 7 '14 at 20:04
  • @skrile modified original answer -- your queries work on my O365 site... are you in an on premises environment?
    – John-M
    Aug 8 '14 at 5:03

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.