Home > CMS- Content Management System > Sharepoint > SharePoint Online Search using Rest API with query text?

SharePoint Online Search using Rest API with query text?

This post was most recently updated on July 31st, 2024

Hello 

Today we will cover SharePoint Search with REST API. In this tutorial, I will explain SharePoint Search API with the Query Text parameter. In my next article, I will cover Query parameters with different data types like SelectProperties, RefinementFilters, Properties etc. The Search REST service supports both HTTP POST and HTTP GET requests. We will cover both requests. You can use the Search REST service to submit Keyword Query Language (KQL) or FAST Query Language (FQL) queries. Free text-keywords—words or phrases.

Before we go ahead I recommended you to download the SharePoint Search Query Tool. This tool will help to build an HTTP search query and debug search queries. You can also try different data types of query parameters. It can be used to query both SharePoint 2013/2016 On-Premise and SharePoint Online.

The tool has multiple authentication methods Sharepoint online, WIndows, From based and forefront gateway.

After running the HTTP search query, you can view all types of result sets returned; Primary Results, Refinement Results, Query Rules Results, Query Suggestions, in addition to the actual raw response received from the Search service.

See the below screenshot for more info.

SP Search Tool

GET requests

Construct the URL for query GET requests to the Search REST service as follows:

https://[your site url]/_api/search/query

For GET requests, you specify the query parameters in the URL. You can construct the GET request URL in two ways:

https://[your site url]/_api/search/query?query_parameter=value&query_parameter=value

https://[your site url]/_api/search/query(query_parameter=value&amp;query_parameter=<value>)

POST requests
You can construct the URL for query POST requests to the Search REST service as follows:

https://[your site url]/_api/search/postquery

 Endpoint
GET requests POST requests
https://[your site url]/_api/search/query?querytext=sharepoint https://[your site url]/_api/search/postquery

I used SP.RequestExecutor object (sp.requestexecutor.js) for a Web request to the server and get a response from the server. I just added two different code snippet for GET and POST method which will call on jQuery click event. I just get only search results “Title” and “Link” values, both are the simple examples.

Both requests have a different response object. You can add column name to return specific column in “selectproperties”.

GET Method

Below screenshot shows the search results of GET method response object.

GET Method Response Object

POST method

For POST requests, you can pass the query parameters in the request in JavaScript Object Notation (JSON) format.

The HTTP POST version of the Search REST service supports all parameters supported by the HTTP GET version. However

Below screenshot shows the search results of POST method response object.

POST Method Response Object

In my next article, I will explain different data types like SelectProperties, RefinementFilters, Properties with an example.

Hope you find it helpful. If you liked this article, then please share and comment. Your comment and suggestion will be greatly appreciated.

This Article is TAGGED in , , , , , , , . BOOKMARK THE permalink.

Avatar photo
Chintan Upadhayay
I am a senior web developer with more than 4+ years experience in WordPress, PHP, MySQL, HTML, CSS, jQuery and other web technologies. I am the beginner in SharePoint and Learning SharePoint.

2 thoughts on “SharePoint Online Search using Rest API with query text?

  1. This article looks great. could you also talk about the authentication mechanism. i want to use these api calls from .net core application. what are the things i need to create to get access to my sharepoint site. i am more looking towards a token with app id and secret rather than user token. let me know. thank you

    1. Hi Vishwanadha,

      The web application is running on a different domain then SharePoint and then you’ll face cross-domain issues. There are a few approaches to solve this:
      1.CORS (Cross-origin resource sharing)

      2.JSONP (JSON with padding)

      3.IFrame with PostMessage API

      4. Use CSOM for Search
      In order to use any manipulation in SharePoint you must obtain an access token.
      Read this articles https://www.vlieshout.net/how-to-gather-data-from-sharepoint-using-rest-api-on-external-website/

      https://www.techsupper.com/2019/05/access-sharepoint-online-rest-apis.html

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">