3

I have a standard document library with one added field StudyId (string). I can't figure out how to retrieve files using a filter on that field.

I retrieve files as following:

http://test-site/_api/web/lists/GetFolderByServerRelativeUrl('/tmgt/StudyDocuments')/Files

if I add a filter ?$filter=StudyId eq 123456 I get an error that StudyId field doesn't exist.

For file items fields are returned as properties, so I guess filtering is different, but I can't find out the proper way

Any ideas?

2

This error occurs since filter expression could not be applied for File Collection resource. From another hand, StudyId field is a property of List Item resource and not a File.

Having said that, you could utilize the following query to return File with StudyId set to 123456:

/_api/web/lists/getbytitle('StudyDocuments')/items?$select=File&$expand=File&$filter=StudyId eq 123456 

It is assumed that StudyDocuments is a Documents library title.

1

Files endpoint doesn't support filtering. Hence you should use CAML query along with REST endpoint

Url -

http://test-site/_api/web/lists/getbytitle('StudyDocuments')/getitems

Body -

{
    'query' : {
        '__metadata' : { 'type' : 'SP.CamlQuery' },
        'ViewXml' : '<View><Query><Where><Eq><FieldRef Name="StudyId"/><Value Type="Integer">123456</Value></Eq></Where></Query></View>',
        'FolderServerRelativeUrl' : '/tmgt/StudyDocuments'
    }
}
0

Libraries in SharePoint are a type of list. So, to query the library you use the same approach as querying a list.

MSDN: https://msdn.microsoft.com/EN-US/library/office/fp142385.aspx

Query the list (library):

_api/web/lists/getbytitle('StudyDocuments')/items?$filter=...

For each item returned, get the file properties

_api/web/lists/getbytitle('StudyDocuments')/items/getbyid(###)/file

One of the properties returned is ServerRelativeUrl

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.