FetchXML v/s QueryExpression

By | April 9, 2010

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
Inner join
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
 
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.
Hope this helps to smart querying!!!

70% of global 2000 companies apply gamification to improve productivity and returns!

Gamifics365 – Spin the magic of games within Microsoft Dynamics 365 CRM to improve user adoption, enhance productivity, and achieve company goals!