Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find a list of Files using Sharepoint REST API

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.

like image 838
john Avatar asked Jan 07 '19 18:01

john


People also ask

How do I use REST API in SharePoint?

To use the REST capabilities that are built into SharePoint, you construct a RESTful HTTP request by using the OData standard, which corresponds to the client object model API you want to use. The client. svc web service handles the HTTP request and serves the appropriate response in either Atom or JSON format.


2 Answers

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!

like image 148
Alberto Avatar answered Oct 05 '22 19:10

Alberto


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.

like image 24
Jamie Keefer Avatar answered Oct 05 '22 21:10

Jamie Keefer