Sharepoint javascript Search.asmx QueryEx
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 = "<QueryPacket xmlns='urn:Microsoft.Search.Query'><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>";
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>