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-
- <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
-
- <script language="javascript" type="text/javascript">
-
- $(document).ready(function() {
- var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
- $.getScript(scriptbase + "SP.Runtime.js", function () {
-
- $.getScript(scriptbase + "SP.js", function(){
-
- $.getScript(scriptbase + "SP.search.js", getSearchResults);
-
- });
- });
-
- });
-
- var results;
- function getSearchResults() {
-
-
- var clientContext = new SP.ClientContext();
- oWeb = clientContext.get_web();
-
-
- var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(clientContext);
- keywordQuery.set_queryText("Demo:Done");
-
-
- var displayProperties = keywordQuery.get_selectProperties();
- displayProperties.add('Demo');
- displayProperties.add('EditorOWSUSER');
-
-
- var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(clientContext);
- results = searchExecutor.executeQuery(keywordQuery);
-
-
- clientContext.executeQueryAsync(QuerySuccess, QueryFailure);
- }
-
- function QuerySuccess() {
-
-
-
- $.each(results.m_value.ResultTables[0].ResultRows, function () {
-
- console.log("--------------------------------------------------")
- console.log("Title -> "+this.Title +"\n");
- console.log("Path-> "+this.Path +"\n");
- console.log("Author -> "+this.Author +"\n");
- console.log("Demo -> "+this.Demo +"\n");
- console.log("Modified By (Principal) -> "+this.EditorOWSUSER +"\n");
- });
-
- }
-
- function QueryFailure(sender,args) {
- console.log('Request failed'+ args.get_message());
- }
-
- </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.