« Troubleshooting the Microsoft Dynamics CRM Client for Outlook | Main | CRM Helper Class for Custom ISV ASPX Pages »
June 02, 2009
Client-side Web Service Calls for Microsoft Dynamics CRM 4.0: Parsing The Results
This is the final article on client-side web service calls for Microsoft Dynamics CRM. The previous articles I wrote focused on making javascript calls to the MetadataService and CrmService web services and setting up some generic functions for re-usability. With this article, I will be focusing on the xml responses of these service calls and how you can parse the results. The SDK has some examples of working with the response xml, but I’ve had to do some playing around to get exactly what I was looking for in some of my functions. As with most things involving programming, there are several different ways to parse xml in your javascript, but I’m going to focus on using techniques that have worked the best for me.
Know What You’re Parsing
It should pretty much go without saying, but before even attempting to parse response xml (in ANY language), you really need to have a solid understanding of the structure of the xml returned by the various service methods. Luckily, the SDK has good examples of what the response xml looks like for any of the service methods (refer SDK links in my previous post).
The examples I’ll use in this post are the Retrieve and RetrieveMultiple web service methods – those are the methods I find myself using most often (I also heavily use Fetch).
Error Checking
Before we get into working with data returned from the methods, I want to go over how to handle any errors returned by the web service. Each SDK example has error checking in the sample code, but I think it’s important to highlight here:
// Check for errors.
var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0)
{
var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
alert(msg);
}
The above code simply checks for the existence of the Error node – if it’s not there then the operation carried out successfully.
Retrieve
Let’s start with the Retrieve method. As you should know, the Retrieve method simply returns attributes from a single entity record. Take a look at the response from a retrieve:
<?xml version="1.0" encoding="utf-8"?>
<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>
<RetrieveResponse xmlns="http://schemas.microsoft.com/crm/2007/WebServices">
<RetrieveResult xmlns:q1="http://schemas.microsoft.com/crm/2007/WebServices" xsi:type="q1:contact">
<q1:fullname>Jesper Aaberg</q1:fullname>
<q1:contactid>{4696F8CB-9A1C-DD11-AD3A-0003FF9EE217}</q1:contactid>
<q1:owningbusinessunit>{0EA35030-3EC9-DC11-A8D2-0003FF9EE217}</q1:owningbusinessunit>
</RetrieveResult>
</RetrieveResponse>
</soap:Body>
</soap:Envelope>
The structure of the returned xml has a single RetrieveResult node. Each attribute retrieved from the record is a child node of the RetrieveResult node. There are a couple of different ways to tackle this one and it really depends on what you need your code to do.
First, you may wish to just directly parse the xml by grabbing the value of the desired attribute:
// Display fullname
var fullName = resultXml.selectSingleNode("//q1:fullname").nodeTypedValue;
alert(fullName);
Or, you may wish to programmatically cycle through each attribute returned (useful if you’re not sure of the attributes you’ll be getting back):
// loop through attributes
var results = resultXml.getElementsByTagName('RetrieveResult');
for (i=0;i < results.childNodes.length;i++)
{
var attributeName = results.childNodes[i].tagName;
var attributeValue = results.childNodes[i].nodeTypedValue;
}
The above code creates the RetrieveResult node object and simply cycles through each of the child nodes.
RetrieveMultiple
Now let’s move on to the RetrieveMultiple method. The method generates this response to a successful request:
<?xml version="1.0" encoding="utf-8"?>
<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>
<RetrieveMultipleResponse xmlns="http://schemas.microsoft.com/crm/2007/WebServices">
<RetrieveMultipleResult EntityName="contact" MoreRecords="0" PagingCookie="">
<BusinessEntities xmlns="http://schemas.microsoft.com/crm/2006/WebServices">
<BusinessEntity xmlns:q1="http://schemas.microsoft.com/crm/2007/WebServices" xsi:type="q1:contact">
<q1:fullname>FirstName LastName</q1:fullname>
<q1:contactid>{56914948-991C-DD11-AD3A-0003FF9EE217}</q1:contactid>
</BusinessEntity>
<BusinessEntity xmlns:q1="http://schemas.microsoft.com/crm/2007/WebServices" xsi:type="q1:contact">
<q1:fullname>FirstName LastName</q1:fullname>
<q1:contactid>{4696F8CB-9A1C-DD11-AD3A-0003FF9EE217}</q1:contactid>
</BusinessEntity>
<BusinessEntity xmlns:q1="http://schemas.microsoft.com/crm/2007/WebServices" xsi:type="q1:contact">
<q1:fullname>FirstName LastName</q1:fullname>
<q1:contactid>{368C8B1B-851C-DD11-AD3A-0003FF9EE217}</q1:contactid>
</BusinessEntity>
</BusinessEntities>
</RetrieveMultipleResult>
</RetrieveMultipleResponse>
</soap:Body>
</soap:Envelope>
Each entity record returned is a BusinessEntity node within the BusinessEntities node. This structure is pretty much the same as when working with the API in .NET and you’re dealing with the BusinessEntities collection.
So, you’re performing the RetrieveMultiple. Chances are you’ll want to do some sort of iterative process to work with the results. The following code performs a simple loop through the results:
var results = resultXml.getElementsByTagName('BusinessEntity');
for (i=0;i < results.length;i++)
{
var idValue = results[i].selectSingleNode('./q1:contactid').nodeTypedValue;
var name = results[i].selectSingleNode('./q1:fullname').nodeTypedValue;
}
The above snippet just grabs the contactid and fullname values for each record returned. Obviously what you do with the values depends on what you are developing, but this gives you the main chunk of functionality.
Again, the SDK has pretty good documentation on how to work with the CRM Web Service using Javascript (or JScript, as it is referred to in the SDK), so it’s really worth it to dig a little deeper than I have here.
Cheers!
Posted by Will Wilson on June 02, 2009 at 09:52 PM | Permalink
TrackBack
TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00e54fb34b6f883301156fc4bbb8970c
Listed below are links to weblogs that reference Client-side Web Service Calls for Microsoft Dynamics CRM 4.0: Parsing The Results:
Verify your Comment
Previewing your Comment
Posted by: |
This is only a preview. Your comment has not yet been posted.
The letters and numbers you entered did not match the image. Please try again.
As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.
Having trouble reading this image? View an alternate.




Comments