28 Apr’17

Dynamics 365, EnterpriseLeveraging FetchXML with Dynamics CRM


FetchXML is used to query the Dynamics 365 database. A great advantage of using FetchXML is that it can be easily designed by using Dynamics 365 database and it is easy to understand.

FetchXML tester is available in the XRM Toolbox which can let you test and fetch results quickly. FetchXML also provides facilities like aggregation and grouping of data.

Using FetchXML:

FetchXML must conform to the fetch.xsd schema provided with the Dynamics 365 SDK under the directory SDK\Schemas\fetch.xsd

FetchXML must be built into a query string which can be used with IOrganizationService.RetrieveMultiple to retrieve data from Dynamics 365.

FetchXML query can be saved in Dynamics 365 by creating a SavedQuery record.


From the above sample query, a query can be constructed inside <fetch></fetch> tags. In the above example, I’m retrieving Contact records with the attributes Contact ID which is the Primary Key and the Full Name of the Contact.

In the above query, using count=’3’ will retrieve a maximum of 3 records.

Executing the FetchXML query:

You can simply use the above query in the FetchXML tester provided in the XRM Toolbox and get results or you can construct a query string and use it with IOrganizationService.RetrieveMultiple() and get results in the EntityCollection object in your application.

Using the above query, here’s how I can make it into a query string and retrieve the results in my application.

Then, I can use foreach() to loop through the result returned in the EntityCollection.

FetchXML Aggregation:

The following aggregation functions are provided in FetchXML:

  • sum
  • avg
  • min
  • max
  • count(*)
  • count(attribute name)

Example of max used with aggregation:

In the above example, I’ve used the attribute aggregate=”true” in the FetchXML header and used an aggregate aggregate=”max” with the field Employee which is count of Employees in an Account.

The result returned for the same was:

The above result returned a formatted value of 13000 which is the max for an Account.

Also, notice that the tag is <total></total> since it was the alias I used in the query. Likewise, you can use other aggregates and grouping functions.

Understanding Results of FetchXML:

FetchXML results are returned in the <resultset></resultset> tags as shown in the sample below:

Each record is retrieved under an individual <result></result> tags.


At maximum, a FetchXML query can return not more than 5000 records.

If results are more than 5000, morerecords attribute in the resultset will return “1” and will be shown like below:

In order to fetch further records, you’ll need to use PagingCookie. A sample code has been provided in the SDK under SampleCode\CS\GeneralProgramming\Queries\FetchPagingWithCookie.cs to leverage the same.

Also, if you want to return results with even smaller numbers, you can use paging in the FetchXML tested by using the following:

In the above example, I’m choosing to show only 2 records per page and I’m retrieving page no. 2 of all the records.

Hope this was helpful!

Written by

Priyesh Wagh

Sr. Software Developer

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.

Want to streamline your business processes?

  • This field is for validation purposes and should be left unchanged.

Recent Articles

  • Dynamics 365 Client Diagnostics

    15 March’ 2018

    Main cause of Performance Issues: Bandwidth and latency are the primary characteristics which affects the performance o...

    Read more
  • Closing Blind Shift in MPOS and CPOS in Dynamics 365 for Retail

    15 March’ 2018

    Manual For closing Blind shift in POS: Blind shifts terminate the currently ongoing shift and when logged in a new shif...

    Read more
  • Persistent Filters in the Power BI Service

    13 March’ 2018

    Introduction: The feature is finally release, and it is power BI has announced general availability of persistent filt...

    Read more
  • Cause and Solution for Scribe MSMQ not receiving Message from AX

    10 March’ 2018

    Issue: Microsoft Message Queuing (MSMQ) service running on Server might be unable to receive messages. Therefore, messa...

    Read more
  • User Privileges on SharePoint – D365 Document Integration

    10 March’ 2018

    Overview: Often, we wonder what could be wrong where we see a generic error on Documents in Dynamics 365. Something lik...

    Read more