CRM 2011 Training Series – What’s the difference between FetchXML and QueryExpression?
What’s the difference between FetchXML and QueryExpression?
These are the two distinct ways that you can query CRM using CRM SDK to retrieve the required information.
QueryExpression is the mostly commonly used way for querying information in CRM. Probably because it uses the object oriented style of coding and so you have distinct classes for query, condition, columns etc. Youhave intellisense to support you when writing a query using QueryExpression.
However, it has its own limitations, one of them being the inability to provide a column of a linked entity to be returned as the query result. This perhaps because the Query Expression would return a dynamic entity or a strongly typed base entity and so it is unable to return columns of related entity.
So you are not able to execute a query similar to the following
Select Opportunity.name, Opportunity.estimatedvalue, Account.address1_city from FilteredOpportunity Opportunity
FilteredAccount Account on Opportunity.customerid = Account.accountis
Where account.address1_country = “US”
This is however possible using FetchXML. FetchXML requires the query to be specified in XML format and the resultset is returned in XML format as well. This allows FetchXML to return related entity columns as well as this is just another node in the xml doc.
The above select query can written as follows using FetchXML
<fetch version=”1.0″ output-format=”xml-platform” mapping=”logical” distinct=”false”>
<order attribute=”name” descending=”false”/>
<link-entity name=”account” from=”accountid” to=”customerid” alias=”aa”>
<condition attribute=”address1_country” operator=”eq” value=”US”/>
Note: CRM Views allow you to select columns of related entity to be displayed in the view… how do you think they do it? FectchXML ofcourse. The query of the view created is stored as a FetchXML query in the SavedQuery entity.
CRM also provides the following messages to convert FetchXML to QueryExpression and vice versa.
– FetchXmlToQueryExpression : Converts from FetchXML to query expression.
– QueryExpressionToFetchXml : Converts from query expression to FetchXML.
Note again, if you are to convert a FetchXML that has select columns specified from related entity, to Query Expression, the related entity columns are not included in the QueryExpression columns list.
**For CRM 2011 Training, please check out our CRM Training page. Please share |Like in Facebook if you feel like this page is useful.