Search REST API for SharePoint – Multiple use cases


SharePoint Search API is helpful to create a client side webpart. You can build a complete search experience without refreshing the page with the responsive client side search result. To build such webpart fist we need to understand how the REST API and its parameter work. The search REST API will always return the jSON file. We can then use the jQuery api to parse the result and show the search result in the way wanted.

In this blog I will explain few of sample search API used to search documents within the SharePoint. To understand the basics of Search API follow this link https://docs.microsoft.com/en-us/sharepoint/dev/general-development/sharepoint-search-rest-api-overview

Below are some of the API parameters and search use cases in the SharePoint.

Use this url to get text format
Search documents, pages, list items from anywhere with the 20 results, the below query will sort the results in descending order

https://sitecollection/site/_api/search/query?querytext=’*’&sortlist=’LastModifiedTime:descending’&rowlimit=20

Search all sites and everything and return only selected properties in the JSON. Note that the date return will be fixed format you need to convert it to local date time.

https://sitecollection/site/_api/search/query?querytext=’*’&sortlist=’LastModifiedTime:descending’&rowlimit=20&selectproperties=’Title,ModifiedOWSDATE,EditorOWSUSER,Path,ParentLink,FileExtension,FileName,ServerRedirectedEmbedURL’

Search everywhere any documents with the specific extensions only – doc, pdf, excel, powerpoint, txt and mpp files only

https:// //_api/search/query?querytext=’*’&sortlist=’LastModifiedTime:descending’&rowlimit=20&selectproperties=’Title,ModifiedOWSDATE,EditorOWSUSER,Path,ParentLink,FileExtension,FileName,ServerRedirectedEmbedURL’&refiners=’fileextension’&refinementfilters='(fileExtension:or(“docx”,”pdf”,”doc”,”xls”,”xlsx”,”xlsm”,”ppt”,”pptx”,”mpp”,”csv”,”txt”))’

Search documents only from me (My documents anywhere inside SharePoint) my email in below case is senthamilv@outlook.com, searches only by that user id

https:////_api/search/query?querytext=’*’&sortlist=’LastModifiedTime:descending’&rowlimit=20&selectproperties=’Title,ModifiedOWSDATE,EditorOWSUSER,Path,ParentLink,FileExtension,FileName,ServerRedirectedEmbedURL’&refiners=’EditorOWSUSER,fileextension’&refinementfilters=’and(fileExtension:or(“docx”,”pdf”,”doc”,”xls”,”xlsx”,”xlsm”,”ppt”,”pptx”,”mpp”,”csv”,”txt”) , EditorOWSUSER:(Senthamilv@outlook.com))’

Search only my (specific user) documents from specific URL. In below query it searches only from https:///sites/ms/

https:///sites/_api/search/query?querytext=’*’&sortlist=’LastModifiedTime:descending’&rowlimit=20&selectproperties=’Title,ModifiedOWSDATE,EditorOWSUSER,Path,ParentLink,FileExtension,FileName,ServerRedirectedEmbedURL’&refiners=’EditorOWSUSER,fileextension,Path’&refinementfilters=’and(Path:”https:///sites/ms/*”,and(fileExtension:or(“docx”,”pdf”,”doc”,”xls”,”xlsx”,”xlsm”,”ppt”,”pptx”,”mpp”,”csv”,”txt”) , EditorOWSUSER:(Senthamilv@outlook.com) ))’

Search all my documents from One Drive only

https:////_api/search/query?querytext=’*’&sortlist=’LastModifiedTime:descending’&rowlimit=20&selectproperties=’Title,ModifiedOWSDATE,EditorOWSUSER,Path,ParentLink,FileExtension,FileName,ServerRedirectedEmbedURL’&refiners=’EditorOWSUSER,fileextension,Path’&refinementfilters=’and(Path:”https://mysite..com/personal*”,and(fileExtension:or(“docx”,”pdf”,”doc”,”xls”,”xlsx”,”xlsm”,”ppt”,”pptx”,”mpp”,”csv”,”txt”) , EditorOWSUSER:(Senthamilv@outlook.com)))’

Search all documents modified by (name *)

https://<sitecollection//_api/search/query?querytext='ModifiedBy:senthamil*'&sortlist='LastModifiedTime:descending'&rowlimit=20&selectproperties='Title,ModifiedOWSDATE,EditorOWSUSER,Path,ParentLink,FileExtension,FileName,ServerRedirectedEmbedURL'&refiners='fileextension'&refinementfilters='(fileExtension:or("docx&quot;,"pdf","doc","xls","xlsx","xlsm","ppt","pptx","mpp","csv","txt"))'

Search documents from your team members (peers and manager). For this search you can first find all your team members using my other blog here. Once the team member email addresses are known, those emails can be used as search refiners.

https:////_api/search/query?querytext=’*’&sortlist=’LastModifiedTime:descending’&rowlimit=20&selectproperties=’Title,ModifiedOWSDATE,EditorOWSUSER,Path,ParentLink,FileExtension,FileName,ServerRedirectedEmbedURL’&refiners=’EditorOWSUSER,fileextension’&refinementfilters=’and(fileExtension:or(“docx&#8221;,”pdf”,”doc”,”xls”,”xlsx”,”xlsm”,”ppt”,”pptx”,”mpp”,”csv”,”txt”) , EditorOWSUSER:or (senthamilv@outlook.com,ananana@hotmail.com))’

8 thoughts on “Search REST API for SharePoint – Multiple use cases

    1. Senthamil Post author

      In the sample query I used refiners to filter doc extensions. Similarly you can use Content Type to be items to get list items only

    1. Alex Lima

      I using this: https://.sharepoint.com/_api/search/query?querytext={FileName}&selectproperties=’Filename,Path’&refiners=’fileextension’&refinementfilters='(fileExtension:or(“docx”,”pdf”,”doc”,”xls”,”xlsx”,”xlsm”,”ppt”,”pptx”,”mpp”,”csv”,”txt”))’.
      And I´m able to retrieve all documents from sharepoint. Some documents I don´t have access, but it´s listed. If I click the link shows me a page from sharepoint saying that I don´t hace access (it´s ok), but I dont want to retrieve these files on my search. That´s my problem.

Leave a comment