Find a list of Files using Sharepoint REST API
Asked Answered
D

2

7

This seems to be an aloof question that I can't track down anywhere including here, so I am going to try again to see if someone has a solution. I have a SharePoint 2013 instance that I use the REST API for doing content searches and return to my React front end to display. This works great. However, I now have a problem when I need to only search for a list of specific documents across the whole site not just in a directory or specific list. I can do a content search using the /_api/search/query?queryText="" just fine, but I want to construct the querytext of this API endpoint to only search for doucments within the list I provide.

For example, if I am looking for three documents:

  1. Foo.txt
  2. Bar.doc
  3. Foobar.pdf

I only want these documents and corresponding data (like the RedirectEmbedURL, etc that I get using the search api) not the /_api/web/lists/getByTitle method.

Is there a way to format the querystring to return only specific files?

Thanks.

Derk answered 7/1, 2019 at 18:50 Comment(0)
T
7

To retrieve the list you need to know the Site path and the library name.

Also you need operators to work with filters

Operators

Retrieve all Files inside a list: https:////_api/Web/Lists/GetByTitle('')/Items?$expand=File

example:

https://domain-example.com/sites/site1/site2/etc/_api/Web/Lists/GetByTitle('listtitle')/Items?$expand=File

Here are some examples of filters:

To filter by name you need to expand "FieldValuesAsText" and filter by the property "FileLeafRef" Example here:

https://[site]/web/Lists/GetByTitle('[library_name]')/Items?$filter=substringof('[TEXT_TO_SEARCH]',Title) or substringof('[TEXT_TO_SEARCH]',FileLeafRef)&$expand=File,FieldValuesAsText

I'm also filtering by Title as I don't know if the user needs the title or the name with the extension.

Filter StartsWith:

https://domain-example.com/sites/site1/site2/etc/_api/Web/Lists/GetByTitle('listtitle')/Items?$expand=File&$filter=startswith(Title,'Foo')

Filter "Contains" (substringof)

https://domain-example.com/sites/site1/site2/etc/_api/Web/Lists/GetByTitle('listtitle')/Items?$expand=File&$filter=substringof('T15', Title)

Filter "Search with Related key (ex1 in this case)": (substringof)

https://domain-example.com/sites/site1/site2/etc/_api/Web/lists/GetByTitle('listtitle')/Items?$expand=FieldValuesAsText&$filter=substringof('BES10GHC10BB001', ex1)

Thanks for reading!

Teahouse answered 10/1, 2019 at 15:58 Comment(3)
Here is what I finally came up with to search only the titles: https://[sharepoint site]/_api/search/query?querytext='*'&rowlimit=10&refinementfilters='Title:or("40c91e71-d3b6-4b23-b034-50f6dca76649"%2c"eb5d0a99-d619-411d-9b0e-abcd5430d7da")'&clienttype='ContentSearchRegular' I am now putting in the Select properties I need to get the data I want. I was stuck on the Refinement Filters and trying to use them to get the titles.Derk
@Derk I want to find files by the key "Name" within a Library inside a site but I don't know how to do it, I've tried: "https://[sharepoint site]/_api/web/Lists/GetByTitle([LIBNAME]')/Items?$expand=File&$filter=substringof('filename.doc', File/Name)" But I'm getting the error that the column "File" doesn't exists. :(Teahouse
@Derk Found It, updating answerTeahouse
S
2

I just use:

https://<domain>/sites/<list-title>/_api/files

It includes Url, Name and "childrenCount" to filter folders.

You can use logic in the request to filter.

Surgery answered 16/6, 2020 at 13:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.