List Aggregator App (SPFx)

SharePoint 2013/2016/Online Search using REST API

This SharePoint search tutorial explains, how can we search in SharePoint 2013/2016/online by using the REST API. This will work for both SharePoint Online as well as with SharePoint On-premises.

In this example, we have an input box where the user can put the search term and when user will click on the button, it will display the result in an alert box. You can also have your own code to display the results in tabular format as well.

SharePoint deveopment training course

Before that lets analyze the REST API URL for Search:

var requestUri = _spPageContextInfo.webAbsoluteUrl + “/_api/search/query?querytext=’SharePoint’

The above will search on the basis of keyword ‘SharePoint’. And the maximum length for the query text allowed is 4096 characters.

var requestUri = _spPageContextInfo.webAbsoluteUrl + “/_api/search/query?querytext=’Title:SharePoint’

The above will search on the basis of keyword ‘SharePoint’ in the Title.

var requestUri = _spPageContextInfo.webAbsoluteUrl + “/_api/search/query?querytext=’SharePoint’&selectproperties=’Title,Path’

The above will search for keyword SharePoint and will return Title and Path properties.

var requestUri = _spPageContextInfo.webAbsoluteUrl + “/_api/search/query?querytext=’SharePoint’&selectproperties=’Title,Path’&rowlimit=5

The above will search for keyword SharePoint and will return Title and Path properties and it will return top 5 results.

var requestUri = _spPageContextInfo.webAbsoluteUrl + “/_api/search/query?querytext=’SharePoint’&selectproperties=’Title,Path’&startrow=11&rowlimit=5

The above will search for keyword SharePoint and will return Title and Path properties and also it will return 5 results starting from 11th.

var requestUri = _spPageContextInfo.webAbsoluteUrl + “/_api/search/query?querytext=’SharePoint’&refiners=’filetype’&refinementfilters=’filetype:equals(“pdf”)’

It will search on the keyword SharePoint and will return pdf files only.

But the below code which you can add inside a script editor web part in SharePoint 2013/2016/Online.

<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js”></script>

<script>

$(function(){
$(“#btnClick”).click(function(){
var queryText = $(“#searchBox”).val();
//var requestUri = _spPageContextInfo.webAbsoluteUrl + “/_api/search/query?querytext=’Title:SharePoint’&selectproperties=’Title,Path’&rowlimit=5′”;
//var requestUri = _spPageContextInfo.webAbsoluteUrl + “/_api/search/query?querytext='”+queryText+”‘”&selectproperties=’Title,Path’&rowlimit=5′”;
var requestUri = _spPageContextInfo.webAbsoluteUrl + “/_api/search/query?querytext='”+queryText+”‘&selectproperties=’Title,Path’&rowlimit=5′”;
alert (requestUri);
$.ajax({
url: requestUri,
type: “GET”,
headers: {
“accept”:”application/json; odata=verbose”
},
success: onSuccess,
error: onError
});
function onSuccess(data) {
var items = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;
if(items.length==0)
{
alert(“Nothing found !!!”);
}
for (var i = 0; i < items.length; i++) {
alert(“Title: ” +items[i].Cells.results[2].Value + ” Path: ” + items[i].Cells.results[3].Value);
}
}
function onError(error) {
alert(JSON.stringify(error));
}
});
});
</script>

<input type=”text” value=”SharePoint” id=”searchBox” />

<input type=”button” id=”btnClick” value=”Get Search Results”/>


You may like following SharePoint search & Rest API tutorials:

Hope this will be helpful to you to learn about SharePoint search using rest api.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →