Skip to content

Instantly share code, notes, and snippets.

@kitmenke
Last active December 17, 2015 01:19
Show Gist options
  • Save kitmenke/5527706 to your computer and use it in GitHub Desktop.
Save kitmenke/5527706 to your computer and use it in GitHub Desktop.
JsRender templates for creating SOAP messages to call the QueryEx Search.asmx service
function encodeHTML(str) {
return str.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;').replace(/'/g,'&apos;');
}
var queryXml = $('#tmplQueryXml').render({
'QueryText': $('#txtQueryText').val(),
'Count': 100,
'StartAt': 1
});
var soapMessage = $('#soapQueryEx').render({
'queryXml': encodeHTML($.trim(queryXml))
});
// IE has whitespace after rendering for some reason
// will just keep the trim and change my templates so they look nice
soapMessage = $.trim(soapMessage);
// make the AJAX call to the Search service
$.ajax({
'url': '/_vti_bin/search.asmx',
'contentType': 'text/xml; charset=utf-8',
'type': 'POST',
'data': soapMessage,
'success': function(data, textStatus, jqXHR) {
// do stuff with data
// maybe $(data).find('RelevantResults') ?
},
'error': $.proxy(ajaxError, self)
});
<script type="text/x-jsrender" id="soapQueryEx">
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<QueryEx xmlns="http://microsoft.com/webservices/OfficeServer/QueryService">
<queryXml>{{:queryXml}}</queryXml>
</QueryEx>
</soap:Body>
</soap:Envelope>
</script>
<script type="text/x-jsrender" id="tmplQueryXml">
<QueryPacket xmlns="urn:Microsoft.Search.Query">
<Query domain="QDomain">
<SupportedFormats>
<Format>urn:Microsoft.Search.Response.Document.Document</Format>
</SupportedFormats>
<Context>
<QueryText language="en-US" type="MSSQLFT"><![CDATA[{{:QueryText}}]]></QueryText>
</Context>
<Range>
<Count>{{:Count}}</Count>
<StartAt>{{:StartAt}}</StartAt>
</Range>
<TrimDuplicates>false</TrimDuplicates>
</Query>
</QueryPacket>
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment