Introduction

If you want to populate a datatable object with results retrieved from a FetchXML query, there are several differences in how the results are returned compared with a typical SQL query:

  1. FetchXML results do not include attributes that are not populated in CRM, even if they are explicitly requested in the query.
  2. You can't retrieve the label for a picklist using the attribute name + "name" convention.
  3. Your code has to add additional logic to deal with AliasedValues, EntityReferences, etc.

Description

The attached class has two methods. First, the public ExecuteFetchXml method takes a string representation of a FetchXML query and a reference to a CRM instance organization web service as input parameters, and then it executes the query and returns the results in a datatable. The private getAttributeValue is used to get useful values for the following CRM datatypes:

  1. Microsoft.Xrm.Sdk.EntityReference
  2. Microsoft.Xrm.Sdk.OptionSetValue
  3. Microsoft.Xrm.Sdk.Money
  4. Microsoft.Xrm.Sdk.AliasedValue

Source Code Files

More Information

Building this sample requires the Microsoft Dynamics CRM SDK. Additionally, if you create a solution from scratch and copy the class file into it, you will need to add a reference to the System.Runtime.Serialization assembly.