Skip to main content
2 of 6
added 138 characters in body; edited tags
Nadeem Yousuf-AIS
  • 18.7k
  • 4
  • 29
  • 59

How to create a search using sharepoint JSOM

I'm new to SharePoint and jsom and I'm trying to build up a search using JSOM.

and I tried below code snippets

1st step: Create a page and added following HTML

<label>Search: </label>
   <input id="searchTextBox" type="text"/>
   <input id="searchButton" type="button" value="Search"/> </div> ​ 
<div id="resultsDiv">​​<br/></div> ​​​​<br/>

I linked jquery and abc.js file succesfully and added this JS code to abc.js file

'use strict';
 
var results;
 
var context = SP.ClientContext.get_current();
var user = context.get_web().get_currentUser();
 
// This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
$(document).ready(function () {
 
    $("#searchButton").click(function () {

       alert('check1');
        var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(context);
        keywordQuery.set_queryText($("#searchTextBox").val());

       alert('check2');
 
        var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(context);
        results = searchExecutor.executeQuery(keywordQuery);
 
        context.executeQueryAsync(onQuerySuccess, onQueryFail)
    });
});
 
 
function onQuerySuccess() {
    $("#resultsDiv").append('<table>');
 
    $.each(results.m_value.ResultTables[0].ResultRows, function () {
        $("#resultsDiv").append('<tr>');
        $("#resultsDiv").append('<td>' + this.Title + '</td>');
        $("#resultsDiv").append('<td>' + this.Author + '</td>');
        $("#resultsDiv").append('<td>' + this.Write + '</td>');
        $("#resultsDiv").append('<td>' + this.Path + '</td>');
        $("#resultsDiv").append('</tr>');
    });
 
    $("#resultsDiv").append('</table>');
}
 
function onQueryFail(sender, args) {
    alert('Query failed. Error:' + args.get_message());
}

In this code check1 alert is coming but not the check2 alert.. Whats the wrong with this..?