"тнιѕ вℓσg ¢συℓ∂ ѕανє уσυя мσηєу ιƒ тιмє = мσηєу" - ∂.мαηנαℓу

Tuesday 22 February 2011

Fetch XML in MS Dynamics CRM 2011

 MS Dynamics CRM 2011 Users could easily download fetch xml queries from the 'Advanced Find'  option. So Developers could make use of the fetch XML as a good user input. This post explains how it could be done. Consider the scenario of contacts.


We would like to build a query to list the contacts who lives in London.  So we navigated to Adavanced Find option, which is one of the best features of MS Dynamcis CRM.

Please note the option to download the fetchxml file


Once you click on the Download Fetch XML option, you could save the XML file to your local machine. The XML file contents is purely based on the query that the user made. For instance our query was based on the city field.


Another example could be if you want to retrieve all account records then the XML file contents would be
<fetch mapping='logical'> 
   <entity name='account'><all-attributes/></entity>
</fetch>

But anyway MS Dynamics CRM will create the XML file for you.

So it is possible to use this XML contents as a user input on any of the forms. Users could easily copy this XML contents and paste in the text box.

We have 2 classes in MS Dynamics CRM which are very useful to deal the Fetch XML contents as normal query expression.

FetchXmlToQueryExpressionRequest   ---- Request to the organization service
 FetchXmlToQueryExpressionResponse -- Response from the organization service

FetchXmlToQueryExpressionRequest req = new FetchXmlToQueryExpressionRequest();
req.FetchXml = "<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false"><entity name="contact"><attribute name="fullname" />attribute name="telephone1" /><attribute name="contactid" /><order attribute="fullname" descending="false" /><filter type="and"><condition attribute="address1_city" operator="eq" value="London" /></filter></entity></fetch>";


FetchXmlToQueryExpressionResponse resp = (FetchXmlToQueryExpressionResponse)service.Execute(req);

So the resp variable will have the results as per the query ( In this case query in the form of fetch XML)



OR


string fetchXML="<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false"><entity name="contact"><attribute name="fullname" />attribute name="telephone1" /><attribute name="contactid" /><order attribute="fullname" descending="false" /><filter type="and"><condition attribute="address1_city" operator="eq" value="London" /></filter></entity></fetch>";


EntityCollection resp = service.RetrieveMultiple(new FetchExpression(fetchXML));

No comments:

Post a Comment