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?
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With