We can use JSOM for keyword query search in SharePoint 2013. I’ll explain the basics of this using CEWP and jQuery.

Prerequisites: Search should be configured and working in the system.

Solution



Figure 1: JSOM Search
  1. <script src=”//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js”></script>
  2. <script type=”text/javascript” src=”/_layouts/15/sp.runtime.debug.js”></script>
  3. <script type=”text/javascript” src=”/_layouts/15/sp.js”></script>
  4. <script type=”text/javascript” src=”/_layouts/15/SP.RequestExecutor.js”></script>
  5. <script type=”text/javascript” src=”/_layouts/15/SP.search.js”></script>
  6. <style>
  7. table.sresult, th.sresult , td.sresult {
  8. border: 1px solid grey;
  9. border-collapse: collapse;
  10. padding: 5px;
  11. }
  12. table.sresult tr:nth-child(odd) {
  13. background-color: #f1f1f1;
  14. }
  15. table.sresult tr:nth-child(even) {
  16. background-color: #ffffff;
  17. }
  18. </style>
  19. <script type=”text/javascript”>
  20. $(function ()
  21. {
  22. var results;
  23. $(“#btnSearch”).click(executeSearch);
  24. $(“#btnSearch”).button();
  25. });
  26. //SERCH FUNCTION
  27. function executeSearch(event)
  28. {
  29. var appweburl = _spPageContextInfo.siteAbsoluteUrl;
  30. var clientContext = new SP.ClientContext(appweburl);
  31. var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(clientContext);
  32. keywordQuery.set_queryText($(“#searchTextBox”).val());
  33. var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(clientContext);
  34. results = searchExecutor.executeQuery(keywordQuery);
  35. clientContext.executeQueryAsync(onQuerySuccess, onQueryFailed);
  36. }
  37. function onQuerySuccess(sender, args)
  38. {
  39. $(“#result_box”).empty();
  40. var table = $(“
  41. <table class=’sresult’>”, { ID: “resultsTable” });
  42. table.append($(“
  43. <thead>”)
  44. .append($(“
  45. <td>”).text(“Title”))
  46. .append($(“
  47. <td>”).text(“Path”))
  48. .append($(“
  49. <td>”).text(“Owner”))
  50. .append($(“
  51. <td>”).text(“Modified”)));
  52. $.each(results.m_value.ResultTables[0].ResultRows, function ()
  53. {
  54. table.append($(“
  55. <tr>”)
  56. .append($(“
  57. <td>”).append(this.Title))
  58. .append($(“
  59. <td>”).append(this.Path))
  60. .append($(“
  61. <td>”).append(this.Author))
  62. .append($(“
  63. <td>”).append(this.Write)));
  64. });
  65. $(“#result_box”).append(table);
  66. }
  67. function onQueryFailed(sender, args) {
  68. $(“#result_box”).empty();
  69. $(“#result_box”).text(“Error: ” + ‘Request failed. ‘ + args.get_message() + ‘\n’ + args.get_stackTrace());
  70. }
  71. </script>
  72. <div>
  73. <label for=”searchTextBox”>Search: </label>
  74. <input id=”searchTextBox” type=’text’ style=”width: 200px”></input>
  75. <input id=”btnSearch” type=’button’ style=”width: 100px” value=”Search”></input>
  76. <br/>
  77. <div id=”result_box”></div>
  78. </div>
JS explained

JS starts with required script references (jQuery, sp.js, sp.search.js etc.) and style for search results table. Followed by javascript function ‘executeSearch’ to get client context, read ‘keyword’ and then execute search query on keyword. ‘onQuerySuccess’ function will handle success case and create a table with search results that will be displayed on the page. ‘onQueryFailed’ will display error message if ‘executeQueryAsync’ fails. Finally a text box to provide ‘keyword’, button to search and div for search results are added as html.

Now upload this ‘Search_JSOM.js’ to site assets or any other library of SharePoint site.

Edit Content Editor Web part on page and provide URL of ‘Search_JSOM.js’ file as Content Link. Save the page, it will look like the following:


Figure 2: Content Editor

Enter a valid text that exists in SharePoint and already crawled, and click on search button; relevant results will be displayed as a table.


Figure 3: Valid Text