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 thecontact
entity as the target.
<attribute>
: Specifies which fields (attributes) to retrieve from thecontact
entity.name='fullname'
: Retrieves thefullname
attribute.name='emailaddress1'
: Retrieves theemailaddress1
attribute.
<link-entity>
: Joins related entities to include data from another table. Here:name='account'
: Joins theaccount
entity.from='accountid'
: The key in theaccount
entity used for the join.to='parentcustomerid'
: The key in thecontact
entity to match with theaccountid
.alias='account'
: Provides an alias for the linked entity.
<filter>
: Defines criteria to filter the data from the linkedaccount
entity. Here:attribute='accountid' operator='eq' value='ACCOUNT_ID'
: Filters the query to only include records where theaccountid
matches the specified value (ACCOUNT_ID
).
What This Query Does
- Target Entity: The query starts by focusing on the
contact
entity. - Attributes to Retrieve: It retrieves the
fullname
andemailaddress1
fields of thecontact
records. - Join Operation: It links the
account
entity to filter contacts based on related accounts. - Filter Condition: Only includes contacts where the
accountid
of theaccount
entity matches the specified value (ACCOUNT_ID
).
Result
The result of this FetchXML query will be:
- A collection of
contact
records. - Only contacts linked to the specified
account
(ACCOUNT_ID
). - Each record will include the
fullname
andemailaddress1
attributes.
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.