javascriptsharepointsharepoint-2013sharepoint-onlinesharepoint-search

Get the internal id for a refineablestring


I'm building up a query which will redirect the user to the search page with the relevant query info. My problem is the only way I know how to get the internal id for the refinablestring is via the address bar, I need a way to be able to get the internal id via JavaScript.

When I say internal ID I mean:

Name: Refinablestring00

Internal id: ǂǂ446f63756d656e7460547970652031

Query that gets generated (decoded):

/sites/example/pages/Search.aspx#Default={"k":"*","r": 
[{"n":"RefinableString00","t": 
["\"ǂǂ4469736363706c696e652032\""],"o":"and","k":false,"m":null}]}

To clarify, I want to be able to get the internal id and I have access to JSOM/client side. What options do I have?

Thanks,


Solution

  • This is not officially documented, but here we go. Let's take a look how refiner filter is represented:

    { 
         "k": queryText,    //search query 
         "r": [   //<- the list of refiners
                  { 
                      "n": propertyName,   //property value 
                      "t": [token],  //encoded property value (see below for a more details)  
                      "o": "and",    //(or,and) operators
                      "k": false, 
                      "m": null 
                  }
          ],
          //another refiners go here.. 
          "l": lcid   //language 
    } 
    

    where token represents the encoded property value which could be generated like this:

    var strToHex = function (value) {
         var hex = unescape(encodeURIComponent(value))
            .split('').map(function(v){
                 return v.charCodeAt(0).toString(16)
            }).join('')
         return hex; 
    };
    
    
    //Usage
    var propertyValue = "Jon Doe";
    var token = "\"ǂǂ" + strToHex(propertyValue) + "\"";
    console.log(token);

    Example

    The following example demonstrates how to generate search url which includes filter for refiner with property name DisplayAuthor and value Jon Doe

    function createRefiner(queryText,propertyName, propertyValue,lcid) {
         lcid = lcid || 1033;
         var strToHex = function (value) {
                    var hex = unescape(encodeURIComponent(value))
                        .split('').map(function(v){
                             return v.charCodeAt(0).toString(16)
                       }).join('')
                    return hex; 
         };
         var token = "\"ǂǂ" + strToHex(propertyValue) + "\"";
         return { 
                  "k": queryText, 
                  "r": [{ "n": propertyName, "t": [token], "o": "and", "k": false, "m": null }], 
                  "l": lcid 
         };
    }
    
    
    //Usage
    var refiner = createRefiner("*","DisplayAuthor","Jon Doe");
    var queryGroupName = "Default";
    var refinerFilter = queryGroupName + '=' + encodeURIComponent(JSON.stringify(refiner));
    var pageUrl = "/_layouts/15/osssearchresults.aspx" + '#' + refinerFilter;
    console.log(pageUrl);