I'm using SPServices to access the SharePoint Search web service. When it returns multiple results my code function fine. However, when it returns on a single result the str.find("Document").length returns 0.
Here's the function:
function filterDataWS(){
var queryText = "<QueryPacket xmlns='urn:Microsoft.Search.Query' Revision='1000'>"
queryText += "<Query>"
queryText += "<Context>"
queryText += "<QueryText language='en-US' type='STRING'>"
queryText += "ContentClass:\"STS_Site\" ContentClass:\"STS_Web\" Title:\"" + $('#webSearch').val().toLowerCase() + "*\"";
queryText += "</QueryText>"
queryText += "</Context>"
queryText += "</Query>"
queryText += "</QueryPacket>";
$().SPServices({
operation: "Query",
queryXml: queryText,
completefunc: function(xData, Status) {
availableTags = new Array();
$(xData.responseXML).find("QueryResult").each(function() {
var x = $("<xml>" + $(this).text() + "</xml>");
//let's see what the response looks like
//$("#result").text($(this).text());
//traverse the xml to get the items
x.find("Document").each(function() {
url = $("Action>LinkUrl", $(this)).text();
//when using keyword syntax
title = $("Title", $(this)).text();
//end keyword syntax
availableTags.push({label: title, value: url});
});
$("#webSearch").autocomplete({
source: availableTags,
select: function(value, data){
$('#goToWeb').attr('href', data.item.value).attr('target', '_blank').css('display', 'inline');
$('#webSearch').val(data.item.label);
return false;
},
search: function(event, ui){
$('#goToWeb').css('display','none');
}
});
});
}
});
}
Here's a working ResponsePacket XML blob:
<ResponsePacket xmlns="urn:Microsoft.Search.Response">
<Response>
<Range>
<StartAt>1</StartAt>
<Count>2</Count>
<TotalAvailable>2</TotalAvailable>
<Results>
<Document relevance="100000000" xmlns="urn:Microsoft.Search.Response.Document">
<Title>FBI</Title>
<Action>
<LinkUrl size="102147">http://sp2010/FBI</LinkUrl>
</Action>
<Description />
<Date>2012-10-18T14:33:11</Date>
</Document>
<Document relevance="100000000" xmlns="urn:Microsoft.Search.Response.Document">
<Title>FEMA</Title>
<Action>
<LinkUrl size="102249">http://sp2010/FEMA</LinkUrl>
</Action>
<Description />
<Date>2012-10-18T14:33:11</Date>
</Document>
</Results>
</Range>
<Status>SUCCESS</Status>
</Response>
</ResponsePacket>
Here's the full response XML. The QueryResult has an encoded value.
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<QueryResponse xmlns="urn:Microsoft.Search">
<QueryResult><ResponsePacket xmlns="urn:Microsoft.Search.Response"><Response><Range><StartAt>1</StartAt><Count>1</Count><TotalAvailable>1</TotalAvailable><Results><Document relevance="100000000" xmlns="urn:Microsoft.Search.Response.Document"><Title>FBI</Title><Action><LinkUrl size="102147">http://sp2010/FBI</LinkUrl></Action><Description /><Date>2012-10-18T14:33:11</Date></Document></Results></Range><Status>SUCCESS</Status></Response></ResponsePacket></QueryResult>
</QueryResponse>
</soap:Body>
</soap:Envelope>
If you remove one of the Document elements, it returns a length of 0.
Any idea of how to resolve this?