Search code examples
sharepoint-2013sharepoint-rest-api

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.


Solution

  • 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!