Add properties in Search results to displayfor JSOM

Hi,

Im developing a search application using JSOM.

Im using KeywordQuery method and its working fine. Im able to search and display the results.

But while displaying im getting limited number of columns to display. like ( Title, Path , Write ..)

I want to display more columns , while researched i found that by default the number of columns are limited.

And if we need then we can add the needed columns in our code. I found CSOM code like below .

But i want the same in JSOM..

KeywordQuery keywordQuery = new KeywordQuery(ctx); keywordQuery.QueryText = keywordQueryValue; keywordQuery.SelectProperties.Add("Description"); keywordQuery.SelectProperties.Add("WebTemplate");

I want the above in JSOM to display my needed columns from search results.

I have made the coulmns ( Managed properties as searchable , Querable , Retreivable etc)

Thanks in Advance.

Regards,

Siva

September 3rd, 2015 11:22am

Hi,

If you want to add custom managed property in search results using JSOM, please refer to the simple demo below:

var context = SP.ClientContext.get_current();

var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(context);

keywordQuery.set_queryText("Search Query");

// Set Properties

var properties = keywordQuery.get_selectProperties();

properties.add('Size');

properties.add('CustomManagedProperty');

var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(context);

var results = searchExecutor.executeQuery(keywordQuery);

context.executeQueryAsync(onQuerySuccess, onQueryFail);

function onQuerySuccess() {

         $(<span>"#resultsDiv"</span>).append('table');   

         if (results.m_value.ResultTables) {

                   $.each(results.m_value.ResultTables, function(index, table) {  

                            if(table.TableType == "RelevantResults") {

                                     $.each(results.m_value.ResultTables[index].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'</span>);

                                               $("#resultsDiv").append('td' + this.Size + '/td');   

                                              $("#resultsDiv").append('td' + this.CustomManagedProperty + '/td');

                                               $("#resultsDiv").append('/tr');

                                     })  

                            }

                   });  

         }

         $("#resultsDiv").append('/table');   

}

Here are some demos about JSOM about search, which are useful to you:

https://blogs.perficient.com/microsoft/2014/01/sharepoint-2013-search-javascript-csom-primer/

Thanks,

Dean Wang

Free Windows Admin Tool Kit Click here and download it now
September 4th, 2015 9:42am

Hi Dean,

Thank you for your response. And this is working fine for me. ( If the script is added in a content editor webpart)

But now am facing a different issue,  I tried to convert this script into an APP . I deployed the app as Tenant scope and im facing issue in getting the result values.

the above script works fine in Content editor webpart but when i converted it into an app am getting error results is undefined.

Any guess what would be the possible root cause.

My code looks like 

 SP.SOD.registerSod('sp.search.js', "/_layouts/15/sp.search.js");  
  
        SP.SOD.executeFunc('sp.search.js', 'Microsoft.SharePoint.Client.Search.Query.KeywordQuery', function() {  

            var ctx = new SP.ClientContext(appweburl);//Get the SharePoint Context object based upon the URL

            var appCtxSite = new SP.AppContextSite(ctx, hostweburl);

            var web = appCtxSite.get_web();

            ctx.load(web);
            

        var queryStr = "Title:'secure'";
       // ctx = new SP.ClientContext.get_current();
        var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(ctx);

        keywordQuery.set_trimDuplicates(false);
        keywordQuery.set_queryText(queryStr);
       

        var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(ctx);
        results = searchExecutor.executeQuery(keywordQuery);
        ctx.executeQueryAsync(onSSSQuerySuccess, onSQueryFail);
        });


function onSSSQuerySuccess() {

        $("#searchResults").append('<table>');

    $.each(results.m_value.ResultTables[0].ResultRows, function () {
        $("#searchResults").append('<tr>');
        $("#searchResults").append('<td>' + this.Title + '</td>');
        $("#searchResults").append('<td>' + this.Write + '</td>');
        $("#searchResults").append('<td>' + this.Path + '</td>');

        $("#searchResults").append('</tr>');
    });

    $("#searchResults").append('</table>');
}
Which gives results is undefined. Results are not loaded.

Thanks in Advance.

Regards,

siva



September 7th, 2015 7:35am

Hi Siva,

Please try to change the code results = searchExecutor.executeQuery(keywordQuery); to var results = searchExecutor.executeQuery(keywordQuery); and test whether error occurs.

Feel freely to reply if there is any update.

Thanks,

Dean Wang

Free Windows Admin Tool Kit Click here and download it now
September 7th, 2015 10:22pm

Hi Dean,

Thanks a lot for your support .

Yes this issue was due to variable declarartion. I have wrongly declared results as local var , when i moved it to global issue resolved.

Once again Thanks a lot for your kind support.

Regards,

Siva

September 8th, 2015 1:45am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics