FetchXML is a proprietary XML-based query language used to retrieve data from Microsoft Dataverse. It's widely used in applications like Microsoft Dynamics 365 and the Power Platform. FetchXML supports a wide range of operations such as filtering, sorting, aggregation, joins, and more.
This document covers all types of FetchXML queries, from basic retrieval to advanced queries including aggregations and joins. We will explore each aspect of FetchXML in detail, providing examples where applicable.
Ask the user to enter their dataverse org url, if they have not mentioned the same.
The user provided table(entity) name might not be the exact dataverse entity logical name. Use 'get_all_entity_names' to get a list of all entites present on the user's dataverse org and select the one entity that matches the most with the name provided by the user. While selecting, discard Microsoft internal tables and focus on user tables. Pass the org url to this function.
The user provided attributes may not be accurate or the user may not have provided the attribues. To get all details such as attribute names and their types, use 'get_entity_metadata'. Pass the exact dataverse logical name and the org url to this function.
Before responding the user with the query, test the query using 'run_fetchxml_query' function. Pass the candidate Fetch XML and the org url to this function and it will return the results after executing the query or the error that may have occoured.
- What is FetchXML?
- Basic FetchXML Structure
- Attributes in FetchXML
- Conditions and Filtering
- Equality and Inequality Operators
- Range Filters
- Null Checks
- Logical Operators
- Joins in FetchXML
- Sorting Data
- Paging and Limiting Results
- Aggregations and Grouping
- Aliases and Projections
- FetchXML with Functions
- FetchXML Advanced Options
- Sample Queries
FetchXML is a query language specifically designed for Microsoft Dataverse, which allows users to retrieve and manipulate data through XML-based queries. These queries can be run through the Dataverse API, in the Dynamics 365 platform, or in PowerApps.
FetchXML queries are structured using XML tags and support a wide variety of operations, including:
- Filtering records
- Sorting results
- Aggregating data
- Fetching related records via joins
A basic FetchXML query consists of a root <fetch>
tag and an <entity>
tag that specifies which entity (table) you want to query. Within the <entity>
, you define which attributes (fields) to retrieve and apply filters or sorting as needed.
<fetch>
<entity name="account">
<attribute name="name" />
<attribute name="accountnumber" />
<attribute name="revenue" />
</entity>
</fetch>
In this example, we are fetching the name
, accountnumber
, and revenue
fields from the account
entity.
The <fetch>
tag can have additional attributes to control how the query is executed:
count
: Specifies the maximum number of records to retrieve.page
: Indicates the page number of results to retrieve (used for paging).top
: Limits the number of records returned.distinct
: Ensures unique results.aggregate
: Used when performing aggregations.
<fetch count="10" page="1" distinct="false">
<entity name="contact">
<attribute name="fullname" />
<attribute name="emailaddress1" />
</entity>
</fetch>
This example fetches the first 10 records from the contact
entity, returning the fullname
and emailaddress1
fields.
The <attribute>
tag specifies the fields to retrieve from the entity. You can list multiple attributes in a single query. If you want to fetch all attributes, you can use the all-attributes
tag.
<fetch>
<entity name="account">
<attribute name="name" />
<attribute name="telephone1" />
</entity>
</fetch>
<fetch>
<entity name="account">
<all-attributes />
</entity>
</fetch>
Filtering in FetchXML is done using the <filter>
and <condition>
tags. The <filter>
tag groups conditions together and allows combining them with logical operators like and
and or
.
You can filter records based on equality (eq
), inequality (neq
), greater than (gt
), less than (lt
), etc.
<fetch>
<entity name="account">
<attribute name="name" />
<filter>
<condition attribute="accountnumber" operator="eq" value="12345" />
</filter>
</entity>
</fetch>
<fetch>
<entity name="account">
<attribute name="revenue" />
<filter>
<condition attribute="revenue" operator="gt" value="50000" />
</filter>
</entity>
</fetch>
FetchXML supports range filters like between
and not-between
.
<fetch>
<entity name="account">
<attribute name="revenue" />
<filter>
<condition attribute="revenue" operator="between">
<value>10000</value>
<value>50000</value>
</condition>
</filter>
</entity>
</fetch>
You can check for null or non-null values using null
and not-null
.
<fetch>
<entity name="account">
<attribute name="websiteurl" />
<filter>
<condition attribute="websiteurl" operator="not-null" />
</filter>
</entity>
</fetch>
Use the and
or or
operators in the <filter>
tag to combine conditions.
<fetch>
<entity name="account">
<attribute name="name" />
<filter type="and">
<condition attribute="revenue" operator="gt" value="50000" />
<condition attribute="primarycontactid" operator="not-null" />
</filter>
</entity>
</fetch>
FetchXML allows you to perform inner and outer joins between related entities using the <link-entity>
tag. This is useful for retrieving related records in a single query.
<fetch>
<entity name="account">
<attribute name="name" />
<link-entity name="contact" from="accountid" to="accountid" link-type="inner">
<attribute name="fullname" />
</link-entity>
</entity>
</fetch>
In this query, we are joining the contact
entity to the account
entity, retrieving the fullname
of the contacts associated with each account.
<fetch>
<entity name="account">
<attribute name="name" />
<link-entity name="contact" from="accountid" to="accountid" link-type="outer">
<attribute name="fullname" />
</link-entity>
</entity>
</fetch>
To sort records, use the <order>
tag. You can specify the attribute to sort by and the direction (asc
or desc
).
<fetch>
<entity name="account">
<attribute name="name" />
<order attribute="name" descending="false" />
</entity>
</fetch>
<fetch>
<entity name="account">
<attribute name="name" />
<order attribute="revenue" descending="true" />
</entity>
</fetch>
To limit the number of results returned, use the top
or count
attributes in the <fetch>
tag. Paging can be done by setting the page
and paging-cookie
attributes.
<fetch count="10" page="2">
<entity name="contact">
<attribute name="fullname" />
</entity>
</fetch>
In this example, the second page of 10 records is retrieved.
FetchXML supports aggregations using functions like sum
, avg
, count
, and min/max
. To use these, set the aggregate="true"
attribute in the <fetch>
tag.
<fetch aggregate="true">
<entity name="account">
<attribute name="revenue" aggregate="sum" alias="total_revenue" />
</entity>
</fetch>
You can group data using the groupby
function. This is useful for reporting purposes.
<fetch aggregate="true">
<entity name="account">
<attribute name="industrycode" groupby="true" alias="industry" />
<attribute name="accountid" aggregate="count" alias="account_count" />
</entity>
</fetch>
Aliases can be used to reference fields in your query, especially when dealing with aggregations or joined entities.
<fetch>
<entity name="account">
<attribute name="name" alias="account_name" />
<attribute name="accountnumber" alias="acc_num" />
</entity>
</fetch>
FetchXML can execute functions such as startswith
, endswith
, contains
, and others for text-based filters.
<fetch>
<entity name="contact">
<attribute name="fullname" />
<filter>
<condition attribute="fullname" operator="startswith" value="John" />
</filter>
</entity>
</fetch>
FetchXML includes advanced options for performance optimization and execution control, such as:
no-lock
: Avoids locking the records while reading them.distinct
: Fetches only distinct records.top
: Limits the number of results.
<fetch distinct="true" no-lock="true" top="5">
<entity name="contact">
<attribute name="fullname" />
</entity>
</fetch>
<fetch>
<entity name="account">
<attribute name="name" />
<filter>
<condition attribute="statecode" operator="eq" value="0" />
</filter>
</entity>
</fetch>
<fetch>
<entity name="account">
<attribute name="name" />
<filter>
<condition attribute="revenue" operator="gt" value="100000" />
</filter>
</entity>
</fetch>
This comprehensive guide covers the full range of FetchXML capabilities, including filtering, joins, aggregation, sorting, paging, and advanced querying techniques. Use these examples as templates to build more complex queries for your Dataverse solutions.
<fetch>
<entity name="crd3e_sales">
<attribute name="crd3e_sales1" />
<attribute name="crd3e_salesamount" />
</entity>
</fetch>
{"@odata.context": "https://example.crm.dynamics.com/api/data/v9.2/$metadata#crd3e_saleses(crd3e_sales1,crd3e_salesamount,_transactioncurrencyid_value,transactioncurrencyid,crd3e_salesid,transactioncurrencyid())", "value": [{"@odata.etag": "W/\"1674155\"", "_transactioncurrencyid_value": "8db2faa4-956f-ef11-a66e-6045bd059140", "crd3e_salesid": "78e021cd-8c05-41d3-a9bd-0f01dc69f4b0", "crd3e_sales1": "Sales 5", "crd3e_salesamount": 400.0}, {"@odata.etag": "W/\"1674154\"", "_transactioncurrencyid_value": "8db2faa4-956f-ef11-a66e-6045bd059140", "crd3e_salesid": "6a8a9c60-0e25-423b-bc74-0f75ee8874f3", "crd3e_sales1": "Sales 4", "crd3e_salesamount": 250.0}, {"@odata.etag": "W/\"1674153\"", "_transactioncurrencyid_value": "8db2faa4-956f-ef11-a66e-6045bd059140", "crd3e_salesid": "8cfd7f7d-dcb7-40a1-acc6-534f1a1252e4", "crd3e_sales1": "Sales 3", "crd3e_salesamount": 300.0}, {"@odata.etag": "W/\"1674151\"", "_transactioncurrencyid_value": "8db2faa4-956f-ef11-a66e-6045bd059140", "crd3e_salesid": "b9803167-af8e-497c-8741-c2a49ebe3b6e", "crd3e_sales1": "Sales 1", "crd3e_salesamount": 150.0}, {"@odata.etag": "W/\"1674152\"", "_transactioncurrencyid_value": "8db2faa4-956f-ef11-a66e-6045bd059140", "crd3e_salesid": "20078e1c-fe25-47ab-9348-e0e415fd3ef9", "crd3e_sales1": "Sales 2", "crd3e_salesamount": 200.0}]}