Understanding FetchXML: Breaking Down a Query Example
FetchXML is a powerful XML-based query language for retrieving data from Microsoft Dataverse. Let’s break down a sample FetchXML query and understand its components.
FetchXML Query
<fetch>
<entity name='contact'>
<attribute name='fullname' />
<attribute name='emailaddress1' />
<link-entity name='account' from='accountid' to='parentcustomerid' alias='account'>
<filter>
<condition attribute='accountid' operator='eq' value='ACCOUNT_ID' />
</filter>
</link-entity>
</entity>
</fetch>
Explanation of Components
<fetch>: The root element of the query, containing the definition of what data to retrieve.<entity>: Defines the primary entity for the query. In this example:name='contact': Specifies thecontactentity as the target.
<attribute>: Specifies which fields (attributes) to retrieve from thecontactentity.name='fullname': Retrieves thefullnameattribute.name='emailaddress1': Retrieves theemailaddress1attribute.
<link-entity>: Joins related entities to include data from another table. Here:name='account': Joins theaccountentity.from='accountid': The key in theaccountentity used for the join.to='parentcustomerid': The key in thecontactentity to match with theaccountid.alias='account': Provides an alias for the linked entity.
<filter>: Defines criteria to filter the data from the linkedaccountentity. Here:attribute='accountid' operator='eq' value='ACCOUNT_ID': Filters the query to only include records where theaccountidmatches the specified value (ACCOUNT_ID).
What This Query Does
- Target Entity: The query starts by focusing on the
contactentity. - Attributes to Retrieve: It retrieves the
fullnameandemailaddress1fields of thecontactrecords. - Join Operation: It links the
accountentity to filter contacts based on related accounts. - Filter Condition: Only includes contacts where the
accountidof theaccountentity matches the specified value (ACCOUNT_ID).
Result
The result of this FetchXML query will be:
- A collection of
contactrecords. - Only contacts linked to the specified
account(ACCOUNT_ID). - Each record will include the
fullnameandemailaddress1attributes.
Conclusion
FetchXML provides a flexible and powerful way to query Dataverse data, allowing you to filter, join, and retrieve exactly the data you need. This breakdown of the sample query demonstrates how to leverage FetchXML for complex data retrieval scenarios.