16

I am trying to filter through a list and return metadata of the documents in that filter. How do I return the name of a document from a document library using REST while being able to filter the document library? I can bring back the title field, but that isn't always the name of the document.

I tried doing a select all in my query and still couldn't see it. The closest I can get is _api/Web/lists/getbytitle('MyList')/Items(2)/FieldValuesAsText. If I query this then I can get the name of the item. However, I'm not sure how to do this while filtering the list.

My original query is _api/lists/getbytitle('MyList')/items?$select=Title,Category,ReviewDate&$filter=ReviewDate ge 'today'.

1
  • 1
    For filtering on name you can use web/GetFolderByServerRelativeUrl('{document library name and folder path }')/Files?$filter=Name eq '{document name}' Commented Dec 23, 2014 at 23:48

3 Answers 3

31

How to retrieve document name using SharePoint REST

Since FileRef contains file name, you could utilize the following query to return document names:

Endpoint: /_api/web/lists/getByTitle('<list title>')/items?$select=FieldValuesAsText/FileRef&$expand=FieldValuesAsText

Example

Get documents (names) that have been created from 2014 year:

/_api/web/lists/getByTitle('Documents')/items?$select=FieldValuesAsText/FileRef&$expand=FieldValuesAsText&$filter=Created ge  datetime'2014-01-01T00:00:00'
2
  • @vadim filter data in SharePoint 2013 and SharePoint Online is not always same way. I am trying to filter by filename its not happening in SharePoint Online. Please advice, Thanks, $filter=substringof('publishdoc-prod.js',FileRef) ist not Working SPO but good in On Prem. Tried : /Files?$filter Name eq {'publishdoc-prod.js'} in SPO , but no luck. Commented Apr 11, 2017 at 10:24
  • 1
    @ShivendraPatel : i have used below query in SP Online and its working /_api/web/lists/getByTitle('Doc Library Name')/items?$select=FileRef&$filter=substringof('Test Document Name',FileRef)"; Commented Apr 15, 2019 at 14:13
14

FileLeafRef:

 /_api/web/lists/getByTitle('list')/items?$select=FileLeafRef

File Name:

/_api/web/lists/getByTitle('<list title>')/items?$select=File/Name&$expand=File

Also, for querying a document library by Name of document, use below:

/_api/web/lists/getByTitle('Doc Library Name')/items?$select=FileRef&$filter=substringof('Test Document Name',FileRef)";
1

try this

/_api/web/Lists('ListGUID')/items?$select=FieldValuesAsText/FileRef&$expand=FieldValuesAsText&$filter=FileLeafRef eq 'filename.txt'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.