3

I have retrieve SharePoint search results using javascript on SharePoint Online with following code:

var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(context);
    keywordQuery.set_queryText("queryText here");
    keywordQuery.set_trimDuplicates(false);    
    keywordQuery.set_rowLimit(500);    
    var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(context);
    results = searchExecutor.executeQuery(keywordQuery);

    context.executeQueryAsync(onQuerySuccess, onQueryFail);

I got results successfully in onQuerySuccess:

function onQuerySuccess() {
    $.each(results.m_value.ResultTables[0].ResultRows, function () {

        console.log(this.Title + ' - ' + this.Author);

        // I want managed metadata column value here (same as Title & Author) if managed metadata column is exist in the search result

    });    
}

I got Title, Author, Path properties.

Now I want to get value of column type Managed Metadata if that column is exist in the search result. How can I get it ? No any managed metadata column is fixed. It can be different for all search results or column should not exist in the search result.

3

Finally, find the solution. I have added a property owsmetadatafacetinfo to keyword query:

var properties = keywordQuery.get_selectProperties();
properties.add('owsmetadatafacetinfo');

Then in the query results, I got the null if managed metadata column does not exist for search result.

If metadata column exist then, I got it with column & it's value for current search result with property owsmetadatafacetinfo :

function onQuerySuccess() {
    $.each(results.m_value.ResultTables[0].ResultRows, function () {

        console.log(this.Title + ' - ' + this.Author);

        /* Following will return value like:
           > Carx0020Filter|Car Filter|kpEc4Tt0Bk6aYzIoWUkAng==|YS6tyhuAgUWauGGOBUO7Hg==|#d317325c-2705-4006-8158-eea6f2629618|Lamborghini;#"             
           Here, 'Car Filter' is managed metadata column & 'Lamborghini' is value for current search result */

          console.log(this.owsmetadatafacetinfo);

    });    
}

It's return like:

Carx0020Filter|Car Filter|kpEc4Tt0Bk6aYzIoWUkAng==|YS6tyhuAgUWauGGOBUO7Hg==|#d317325c-2705-4006-8158-eea6f2629618|Lamborghini;#"

Where, Car Filter is managed metadata column (with internal name Carx0020Filter) & Lamborghini is value for current search result.

2

You can get that field value, by specifying managed property name of the same field. See below snippet to add more in get_selectProperties.

// Set Properties
var properties = keywordQuery.get_selectProperties();
properties.add('Size'); 
properties.add('CustomManagedProperty'); // Size, CustomManagedProperty is my custom fields which I want in results. You can specify yours.

Below is the example to get any additional field in Keyword query with JavaScript:

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'); // Specify your field here.

var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(context);
var results = searchExecutor.executeQuery(keywordQuery);
context.executeQueryAsync(onQuerySuccess, onQueryFail);
  • Thanks for the answer. But my requirement is that those column names (Size, CustomManagedProperty) are not fixed. There should be any metadata column whose value I have to get. – Ronak Patel Feb 21 '17 at 10:56
  • Hey Ronak, If those properties are not fixed then how would you identify the variable name (correct managed property name of the field)? Though you can give a try to add Searchable option to managed property field and verify if the result is appearing. for more info: support.office.com/en-us/article/… – Pradip R. Feb 21 '17 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.