Add Managed Property To Search Results In SharePoint 2016 Using Javascript Object Model

If you were to ask ask me what makes a SharePoint Site column searchable, the answer to this question points directly at the very powerful yet simple implementation known as Managed Property.

Let's say, you have a site column called Region. You want to search for a particular region and return the matching list items by typing the value into the search box. Share Point internally creates a property called crawled property (ows_internalname) for each of the columns created in the list. In this case the Region will have the Crawled Property name as ows_Region. Once you create a managed property (Say : managed_Region) and map the crawled property (ows_Region) of SharePoint list against the new managed property, the site column, Region, becomes searchable. By default, the site columns, which will have a crawled property and a managed property, are created automatically but not the list columns .

By default, the managed properties are not included in the search results. Lets see how to add the managed properties as well in the search results. We can use JavaScript Object Model and see how to do this.


Here 'Demo' is the managed property and we will add it to the search result, using JSOM code, given below-

  1. <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>  
  2.   
  3. <script language="javascript" type="text/javascript">  
  4.   
  5. $(document).ready(function() {  
  6. var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";  
  7.  $.getScript(scriptbase + "SP.Runtime.js", function () {  
  8.   
  9.     $.getScript(scriptbase + "SP.js", function(){  
  10.   
  11.          $.getScript(scriptbase + "SP.search.js", getSearchResults);  
  12.   
  13.     });  
  14.  });  
  15.   
  16. });  
  17.   
  18. var results;  
  19. function getSearchResults() {  
  20.   
  21. //Get the client context and the web object  
  22. var clientContext = new SP.ClientContext();  
  23. oWeb = clientContext.get_web();  
  24.   
  25. //Fetch records with managed property value Demo is equal to Done  
  26. var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(clientContext);  
  27. keywordQuery.set_queryText("Demo:Done");  
  28.   
  29. //Get the selected properties of the returned results  
  30. var displayProperties = keywordQuery.get_selectProperties();  
  31. displayProperties.add('Demo');  
  32. displayProperties.add('EditorOWSUSER');  
  33.   
  34. //Initiate the search executor to Execute the search query to return results  
  35. var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(clientContext);  
  36. results = searchExecutor.executeQuery(keywordQuery);  
  37.   
  38. //Execute the batch  
  39. clientContext.executeQueryAsync(QuerySuccess, QueryFailure);  
  40. }  
  41.   
  42. function QuerySuccess() {  
  43.   
  44.   
  45. //Iterate the returned results and print the properties  
  46. $.each(results.m_value.ResultTables[0].ResultRows, function () {  
  47.   
  48. console.log("--------------------------------------------------")  
  49. console.log("Title -> "+this.Title +"\n");  
  50. console.log("Path-> "+this.Path +"\n");  
  51. console.log("Author -> "+this.Author +"\n");  
  52. console.log("Demo -> "+this.Demo +"\n");  
  53. console.log("Modified By (Principal) -> "+this.EditorOWSUSER +"\n");  
  54. });  
  55.   
  56. }  
  57.   
  58. function QueryFailure(sender,args) {  
  59. console.log('Request failed'+ args.get_message());  
  60. }  
  61.   
  62. </script>   

We can add the code, mentioned above, to the Content Editor Web part and get the search result output with the managed property, as shown below-


Summary - Thus, we saw how to add a managed property to SharePoint search results. using JavaScript Object Model.

Ebook Download
View all
Learn
View all