Aggregation in FetchXmlDataSets

FetchXmlDataSets provide functionality beyond the Advanced Search, namely the ability to precompress the data by summating the number of records or values ​​in an entity field.You can get an impression of the FetchXML syntax used when you export a diagram in the CRM system, here as an example. The FetchXML statement of the system diagram 'Companies by industry':

<fetch aggregate="true">
      <entity name="account">
            <attribute groupby="true" alias="groupby_column" name="industrycode" />
            <attribute alias="aggregate_column" name="name" aggregate="count" />

You can copy the above FetchXML into a FetchXmlDataSet as it is and receive the XML output as:

        <aggregate_column formattedvalue="16">16</aggregate_column> 
        <groupby_column name=" Chemische Industrie" formattedvalue="73">73</groupby_column> 
        <groupby_column_industrycodename> Chemische Industrie</groupby_column_industrycodename> 
        <aggregate_column formattedvalue="84">84</aggregate_column> 
        <groupby_column name="IT-Industrie" formattedvalue="74">74</groupby_column> 

That is to say, the CRM system lists 16 companies in the chemical industry, 84 in the IT industry, etc. Please note that all other columns that are not aggregated must contain the groupby clause. The alias attribute is required for aggregations because it is necessary to generate the node name of the XML output.

Aggregation Types

The following aggregations can be specified in the aggregate attribute:

  • count: Counts the number of records based on the specified field
  • sum: Counts the sum of all field values ​​of the specified field (for numbers and currency fields)


<i18n:DataSet Entity="account" ItemName="Account" MultipleItems="true" Limit="10">
   <i18n:LinkedFetchXmlDataSet ItemName="Contact" MultipleItems="true" FromAttribute="accountid">
          <fetch aggregate="true">
            <entity name="contact">
              <attribute name="contactid" alias="AnzahlKontakte" aggregate="count"/>
              <attribute name="gendercode" alias="Geschlecht" groupby="true"/>
              <filter type="and">
                <condition attribute="parentcustomerid" operator="eq" value="@parentid" />
        <i18n:Column Descending="true">numberofemployees</i18n:Column>

The XML output lists the 10 companies with the most employees and the number of contacts by gender:

        <name>Bessel Flamengos GbR</name> 
        <numberofemployees formattedvalue="230">230</numberofemployees> 
                < AnzahlKontakte formattedvalue="29">29</ AnzahlKontakte> 
                <Geschlecht name="Männlich" formattedvalue="1">1</Geschlecht> 
                < AnzahlKontakte formattedvalue="42">42</ AnzahlKontakte> 
                <Geschlecht name="Weiblich" formattedvalue="2">2</Geschlecht> 

Additional DataSets

The following DataSets are available:

  • CallingUserDataSet: A DataSet which contains all data of the calling user.
  • DefaultDataSet: Corresponds to the attributes of the entity and the data for which a document is created, the "primary record". For compatibility reasons, its content is displayed directly under the root node.
  • RenderingContextDataSet: A DataSet containing values of the RenderingContext(For example: system-wide variables)