Sharepoint javascript Search.asmx QueryEx

27/03/2015 11:01

Use following html / js call to get search results from Search.asmx service by using QueryEx method.

- Replace siteUrl in ajax call with your

- Parsing is not correct, but you can see the service response



<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";
  var callResult = [];
  //xml input must be encoded, otherwise we get HTTP 400
  var xmlInput = "<QueryPacket xmlns='urn:Microsoft.Search.Query'><Query><SupportedFormats><Format revision='1'>urn:Microsoft.Search.Response.Document:Document</Format></SupportedFormats><Context><QueryText language='en-US' type='STRING'>" + searchTerm + "</QueryText></Context></Query></QueryPacket>";
  xmlInput = "&lt;QueryPacket xmlns='urn:Microsoft.Search.Query'&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 SetCallResult(resValue){
 callResult = resValue;
}


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> \
    <QueryEx xmlns='https://microsoft.com/webservices/OfficeServer/QueryService'> \
      <queryXml>" + searchTerm + "</queryXml> \
    </QueryEx> \
  </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;
 var resultArray = [];
    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);
    resultArray.push(this.textContent);
        
   });
   SetCallResult(resultArray);
   
        }
    }
    catch (e) {
        alert(e);
    }
}

</script>
<h2>JS search QueryEx 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>