Sharepoint - get the attachement files from REST API
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.
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 =)
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;
});
}
}