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
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

<fetch version=”1.0″ output-format=”xml-platform” mapping=”logical” distinct=”false”>
<entity name=”opportunity”>
<attribute name=”name”/>
<attribute name=”estimatedvalue”/>
<attribute name=”opportunityid”/>
<order attribute=”name” descending=”false”/>
    <link-entity name=”account” from=”accountid” to=”customerid” alias=”aa”>
<attribute name=”address1_city”/>
    <filter type=”and”>
                <condition attribute=”address1_country” operator=”eq” value=”US”/>
    </filter>
    </link-entity>
    </entity>
</fetch>

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.

Advertisements

About Dynamics CRM Guru

We are a group of CRM Consultants and have significant experience in transforming businesses in the right way. We use this platform to share our knowledge which might help many others in the digital transformation journey. We are happy to help you if you are looking for any assistance in CRM implementation projects.

Posted on December 14, 2012, in Microsoft CRM 2011, MS CRM 2011 Training and tagged . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: