Sharepoint javascript Search.asmx GetQuerySuggestions

26/03/2015 15:43

Example how to get search suggestions in Sharepoint 2010 using javascript web service call of Search.asmx

- update your site collection url

- this html page is not supposed to be on sharepoint, if so, you can use relative site collection url from _vti_bin)


<html>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
    var isIE = (navigator.appName.toLowerCase().indexOf('netscape') == -1);
   
    $(document).ready(function() {
        var searchTerm = "test";
  //xml input must be encoded, otherwise we get HTTP 400
  var xmlInput = "<QueryPacket xmlns='urn:Microsoft.Search.Query' Revision='1000'><Query><PreQuerySuggestions>true</PreQuerySuggestions><SupportedFormats><Format>urn:Microsoft.Search.Response.Document.Document</Format></SupportedFormats><Context><QueryText language='en-US' type='STRING'>"+ searchTerm +"</QueryText></Context><Range><Count>10</Count></Range></Query></QueryPacket>";
  xmlInput = "&lt;QueryPacket xmlns='urn:Microsoft.Search.Query' Revision='1000'&gt;&lt;Query&gt;&lt;PreQuerySuggestions&gt;true&lt;/PreQuerySuggestions&gt;&lt;SupportedFormats&gt;&lt;Format&gt;urn:Microsoft.Search.Response.Document.Document&lt;/Format&gt;&lt;/SupportedFormats&gt;&lt;Context&gt;&lt;QueryText language='en-US' type='STRING'&gt;"+ searchTerm +"&lt;/QueryText&gt;&lt;/Context&gt;&lt;Range&gt;&lt;Count&gt;10&lt;/Count&gt;&lt;/Range&gt;&lt;/Query&gt;&lt;/QueryPacket&gt;";
  makeSoapCall(xmlInput);
    });
   

function makeSoapCall(searchTerm){
   var soapEnv =
"<soap:Envelope xmlns:xsi='https://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='https://www.w3.org/2001/XMLSchema' xmlns:soap='https://schemas.xmlsoap.org/soap/envelope/'> \
  <soap:Body> \
    <GetQuerySuggestions xmlns='https://microsoft.com/webservices/OfficeServer/QueryService'> \
      <queryXml>"+ searchTerm +"</queryXml> \
    </GetQuerySuggestions> \
  </soap:Body> \
</soap:Envelope>";
   
document.getElementById("request").innerText = soapEnv;
       
    $.ajax({
        url: "https://yoursitecollection/_vti_bin/Search.asmx",
        type: "POST",
        dataType: "xml",
        data: soapEnv,
        complete: processResult,
        contentType: "text/xml; charset=\"utf-8\""
    });
}

function processResult(xData, status) {
    var result;
 
    if (isIE)
        result = xData.responseXML
    else
        result = xData.responseText
       
    try {
  document.getElementById("serviceCallResult").innerText = result;
        if (status == "success" && result){
   var xmlDoc = $.parseXML(result);
   var ulSearchSuggestions = $("ul");
   $(xmlDoc).find("string").each(function() {
    var newLi = '<li>'+ this.textContent +'</li>';
    ulSearchSuggestions.append(newLi);
        
   });
   
        }
    }
    catch (e) {
        alert(e);
    }
}

</script>
<h2>JS search GetSuggestions service call test</h2>
<h3>Request:</h3>
<div></div>
<br/>
<h3>Response:</h3>
<div id='serviceCallResult'></div>
<h3>Parsed Search suggestions: </h3>
<ul>
</ul>
</html>