How to do SharePoint Search using CSOM in Sharepoint online or Office 365?

Hello SharePointers,

In this blog, we will discuss about Sharepoint Search using CSOM in Sharepoint online or office 365. Below is the code snippet to achieve the same.

var queryText = “Text;
var ctx= new SP.ClientContext(“http://mysite”);
var ctx= ctx.get_site();
var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(clientContext);
keywordQuery.set_queryText(queryText);
keywordQuery.set_rowLimit(500);
keywordQuery.set_trimDuplicates(false);

var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(clientContext);
var results = searchExecutor.executeQuery(keywordQuery);
ctx.executeQueryAsync(onQuerySuccess, onQueryError);

function onQuerySuccess()
{
var message = $(“#message”);
message.text(“Your Results are”” + queryText + “\””);
message.append(“<br />”);
var rows = results.m_value.ResultTables[0].ResultRows;
$.each(rows, function(index, value)
{
message.append(value.Title + “: ” + value.Path);
message.append(“<br />”);
});
}

Happy SharePointing 🙂

Leave a Reply

Your email address will not be published.