Wednesday, November 4, 2009

JavaScript code to parse FetchXMLResult

Recently I came across a scenario, where I was required to query based on an intersect table for which Fetch method can only be used, which required parsing Fetch XML Result string was at the client side.

Fetch method is the query language used in Microsoft Dynamics CRM. It is based on a schema that describes the capabilities of the language. The FetchXML language supports similar query capabilities as query expression.

Following is the code that can be used to achieve the same:
var resultXml = xmlHttpRequest.responseXML;
var objNodeList = resultXml.documentElement.selectNodes("//FetchXmlResult");
var totalNodesCount = objNodeList.length;
if (totalNodesCount > 0)

{
var iter = 0;
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.loadXML(objNodeList[0].childNodes[0].nodeValue);


var objResultList = xmlDoc.documentElement.selectNodes("//result");
for(iter = 0; iter <= totalNodesCount; iter++)

{
// Columns Retrieved can be accessed using child nodes
var strEntityId = objResultList[iter].childNodes[0].text;
var strEntityType= objResultList[iter].childNodes[1].text;
}
}

No comments:

Post a Comment