2

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>&lt;ResponsePacket xmlns="urn:Microsoft.Search.Response"&gt;&lt;Response&gt;&lt;Range&gt;&lt;StartAt&gt;1&lt;/StartAt&gt;&lt;Count&gt;1&lt;/Count&gt;&lt;TotalAvailable&gt;1&lt;/TotalAvailable&gt;&lt;Results&gt;&lt;Document relevance="100000000" xmlns="urn:Microsoft.Search.Response.Document"&gt;&lt;Title&gt;FBI&lt;/Title&gt;&lt;Action&gt;&lt;LinkUrl size="102147"&gt;http://sp2010/FBI&lt;/LinkUrl&gt;&lt;/Action&gt;&lt;Description /&gt;&lt;Date&gt;2012-10-18T14:33:11&lt;/Date&gt;&lt;/Document&gt;&lt;/Results&gt;&lt;/Range&gt;&lt;Status&gt;SUCCESS&lt;/Status&gt;&lt;/Response&gt;&lt;/ResponsePacket&gt;</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?

1
  • Didn't work for me. x.find("Document").each... still returns no rows. Any other suggestions? xData.responseXML is fully populated with documents.
    – user20554
    Commented Nov 4, 2013 at 14:43

1 Answer 1

0

jQuery's parseXML solved the issue. It now works in IE9, Chrome, and FireFox. Here's what it looks like:

$().SPServices({
            operation: "Query",
            queryXml: queryText,
            completefunc: function(xData, Status) {
                availableTags = new Array();

                $(xData.responseXML).find("QueryResult").each(function() {
                    var x = "<xml>" + $(this).text() + "</xml>";
                    var xmlDoc = $.parseXML(x);
                    x = $(xmlDoc);

                        //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');
                        }
                });
            }
        });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.