SharePoint Search Using REST API

In this post, we will learn how we can consume SharePoint Search using REST API. We will build a click-through interface which will act like a refiner and will provide you with refined results which I have further refined at multiple levels. I am displaying the final results in a table.

Sometimes we get a requirement to create our own search functionality using REST APIs where we simply want to display the result in a table or grid and want to modify the search result and refiners based on our requirement. 
So, here I will provide you with detailed steps of consuming SharePoint Search using REST API. As mentioned above, we will build a click-through interface which will act like a refiner to provide refined results. The final results will be shown in a table that will look like the below image.
SharePoint Search Using REST API 
Here, you can see the click-through interface of the search results along with breadcrumbs on the top of the page.
These search results are basically refiners that I am getting by applying filters on the results. These refiners can further be dug into to get the sub-refiners of the refiners.
Step 1
In order to apply refiners in REST API, you need to use the following query.
  1. var spListURL = "'Path:'&rowlimit=500&refiners='Plant'";  
  2. spListURL+="&refinementfilters='Plant:equals(\""+plantFilter+"\")'";   
Here, "Plant" is my custom refiner in my SP Site.
Step 2
In order to display the results from the above query, you need to write the following code in the Success method. 
  1. var refinements = data.d.query.PrimaryQueryResult.RefinementResults.Refiners.results[0].Entries.results;  
  2. var searchRefinementHtml = '';  
  3. $.each(refinements, function(index, refinement) {  
  4.     //searchRefinementHtml += '<a href="#" id="'+refinement.RefinementValue+'" onclick="childMethod(this)" >'+refinement.RefinementValue + " (" + refinement.RefinementCount + ")"+'</a><br/>';  
  5.     searchRefinementHtml += '<a href="#" id="' + refinement.RefinementValue + '" onclick="childMethod(this)" >' + refinement.RefinementValue + '</a><br/>';  
  6. });  
Here, "refinements" will provide you with the collection of subrefiners available under refiners.
The subrefiners collection will look like the following image.
SharePoint Search Using REST API 
Step 3
Once you have got all the subrefiners inside refiners, you can use the following REST API query to get results.
  1. var spListURL = "'Path:'&rowlimit=500&selectproperties='Filename,DocNumber,DocumentType,Business,Plant,Region,LocationType,ProductLine,DocLanguage,DocumentStatus,DraftNo,ProductName,Path,Url'";    
  2. spListURL+="&refinementfilters='and(Plant:equals(\""+plantFilter+"\"),Business:equals(\""+BusinessVal+"\"),ProductLine:equals(\""+ProductLineVal+"\"),ProductName:equals(\"""\"))'";  
Here, I have passed my column managed metadata property using "selectproperties" which I have configured earlier. If you have not configured it yet, you need to configure that first.
Step 4
To bind the returned results with HTML, you can use the below code in Success method.
  1. var itemData = [];  
  2. itemData = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;   
Here, 'itemData' will hold all results including your managed metadata column value.
With the above code, we can achieve search results from our SharePoint site and can later bind that with HTML based on our requirement with different look and feel. You can refer to the complete code attached with this post for your reference.
Happy Coding!!!