views:

713

answers:

5

I am querying the Microsoft Office SharePoint Server Search Service to write some results into a web part. I have the query working correctly but am having some trouble parsing the xml response via JQuery.

Below is the XML response

<ResponsePacket xmlns="urn:Microsoft.Search.Response">
  <Response domain="QDomain">
  <Range>
  <StartAt>1</StartAt> 
  <Count>1</Count> 
  <TotalAvailable>1</TotalAvailable> 
  <Results>
  <Document xmlns="urn:Microsoft.Search.Response.Document">
  <Action>
  <LinkUrl fileExt="aspx">https://mysite.domain.inc:443/Person.aspx?guid=4A4F27E2 9C99 4866 BB08 DE494475A4E7</LinkUrl> 
  </Action>
  <Properties xmlns="urn:Microsoft.Search.Response.Document.Document">
  <Property>
  <Name>TITLE</Name> 
  <Type>String</Type> 
  <Value>Smith, Joseph</Value> 
  </Property>
  <Property>
  <Name>RANK</Name> 
  <Type>Int64</Type> 
  <Value>873</Value> 
  </Property>
  <Property>
  <Name>SIZE</Name> 
  <Type>Int64</Type> 
  <Value>0</Value> 
  </Property>
  <Property>
  <Name>DESCRIPTION</Name> 
  <Type>String</Type> 
  <Value>Hi guys!</Value> 
  </Property>
  <Property>
  <Name>WRITE</Name> 
  <Type>DateTime</Type> 
  <Value>2009 07 31T03:00:24 04:00</Value> 
  </Property>
  <Property>
  <Name>PATH</Name> 
  <Type>String</Type> 
  <Value>https://mysite.domain.inc:443/Person.aspx?guid=4A4F27E2 9C99 4866 BB08 DE494475A4E7</Value> 
  </Property>
  <Property>
  <Name>JOBTITLE</Name> 
  <Type>String</Type> 
  <Value>Programmer</Value> 
  </Property>
  </Properties>
  </Document>
  </Results>
  </Range>
  <Status>SUCCESS</Status> 
  </Response>
  </ResponsePacket>

I'm trying to get the TITLE i.e. Smith, Joseph and the JOBTITLE i.e Programmer using JQuery.

I started with:

$(xml).find('Properties').each(function(){
  //not sure how to get the ones I want, use an indexer?
});
A: 

Is there an option that allows you to get the items back as JSON instead?

Neil Barnwell
Not that I know of, these are the inbuilt search, old ASMX style Web services built into Sharepoint
RandomNoob
+3  A: 

These tutorials seem good: jQuery and XML revisited, Reading XML with jQuery.

If you are able to get the data as JSON (JavaScript Object Notation) you will have it easier as far as using/manipulating your data in JavaScript. And you might see performance-gains, depending on the amount of data.

roosteronacid
+4  A: 

Something like

var title;
var jobTitle;

$('Property Name', 'Properties').each(function() {

  var $this = $(this);
  if ($this.text() === "TITLE") {
    title = $this.nextAll("Value").text();
  }
  if ($this.text() === "JOBTITLE") {
    jobTitle = $this.nextAll("Value").text();
  }

});

return {
            "title" : title,
            "jobTitle" : jobTitle
       }

Here's a Working Demo with your XML.

EDIT:

As noted in the comments, I have made the assumption that the XML is part of the document. If the XML is not part of the document, then change the following line

$('Property Name', 'Properties').each(function() { ...

to

$('Property Name', xml).each(function() {

where xml is the service xml response.

Russ Cam
You might want to add that this code assumes that the XML is part of the document. If not, you will have to do $(xml) for this code to work.
SolutionYogi
thanks. Will update now
Russ Cam
+2  A: 
SolutionYogi
+1  A: 

I came very close to getting it with pure selectors - $(xml).find("Name:contains(TITLE)").nextAll("Value").text() but because you wanted title and jobtitle it broke.

Anyway, I figure I'll throw my solution in there as it's a little different - the main idea is that there's only 1 if to get any key.

function getValue(children, key) {
  var ret;
  children.find("Name").each(function() {
    if($(this).text() == key) {
      ret = $(this).nextAll("Value").text();
      return;
    }
  });
  return ret;
}

var children = $(xml).find("Property");
var name = getValue(children, "TITLE");
var jobTitle = getValue(children, "JOBTITLE");
Andy Gaskell