Saturday, December 4, 2010

CRM 2011 Beta :- Querying linked entities from the system using FetchXml

Many a times there's a requirement on querying the data from entity and joining the data with the linked entity as well. 
e.g. If i have a "Student Event" entity in the CRM system which has a many to one relationship with the "Student" event and if i want to display the "Student Event " details on my UI then obviously i would want to display the "name" attribute of the student on the UI as well. Now if i use RetriveMultiple method to get these details it would take more then one hit on the CRM Server to get all the relevant data. 
To avoid the round trip on the server i can instead use FetchXml method on the CRM Web service. This method would return all the data to me in the form of xml. 
The method takes xml query as input and this xml can be formed using a Stunnware tool which can be downloaded here
Using this tool we can easily create and test the xml which has to be passed to FetchXml method. However there's a catch. If you use this tool to generate the fetch xml it would generate an xml which would look like 




<fetch count="50" mapping="logical" version="1.0">

<entity name="studentevent">

<all-attributes>

<link-entity from="studentid" name="student" to="studentid">

<all-attributes>

</all-attributes></link-entity>

</all-attributes></entity>

</fetch>





and when you execute FetchXml API using this xml you would get all the attributes of the student event entity to be returned however the attributes of the student would not be returned. 
To solve this you need to manually edit the xml and add an alias attribute to the linkentity student. So the final xml should look something like 




<fetch mapping="logical" count="50" version="1.0">

<entity name="studentevent">

<all-attributes />

<link-entity name="student" from="studentid" to="studentid" alias="studentdetails">

<all-attributes />

</link-entity>

</entity>

</fetch>

and this would get you the attributes of studentdetails entity as well.

This may be a bug in the stunnware tool or may be because it was buit to be used against CRM 4.0. Otherwise the tool is an excellent way to generate the xml to query the CRM System with linked entities.

Njoi!!!
~Abhishek

2 comments:

Anonymous said...

Hi Abhishek,
One of my friend was also facing the same issue. He asked me and i remember that i had read the same in your blog.

Thanks,
Nishant Rana

Anuj Govil said...

Hi Abhishek,

Can you please explain about the link entities in details.
I have used them but not sure about the logic.

I will be thankful if you will explain in .net coding too.


Thanks in advance,

Anuj Govil