web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Page large result sets with FetchXML - for aggregate attribute

(1) ShareShare
ReportReport
Posted on by

Hello,

In a plugin in CRM 2015 on-premises, I need to compute the sums for an attribute, for a large number of records (over 5000).

The problem is that when the fetch is executed,  the result collection has more than 5000 records (the fetch contain an aggregate attribute).

I have tried to solve the problem using the technique described in  Sample: Use FetchXML with a paging cookie from SDK, but the value for returnCollection.MoreRecords is false even if there are more than 5000 records in the collection 

I have tested my code with a fetch without aggregation and in this case I can get more than 5000 records (like the sample says). 

Please, how can I get more than 5000 records for a fetch with aggregation ?

Thanks,

Radu Antonache

*This post is locked for comments

I have the same question (0)
  • Ramakanta Profile Picture
    2,715 on at

    Hi Radu,

             I am facing the same issue,did you find any solution?

  • Community Member Profile Picture
    on at

    Hi Radu,

    Use count in fetch XML query.

    <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true" count = "50000" aggregate="true">

    Hope this will resolve your problem. Let me know if you have any concern.

    Thanks!

    Santa

  • Ramakanta Profile Picture
    2,715 on at

    Its giving "Invalid XML" error

  • Community Member Profile Picture
    on at

    Hi RK,

    Can you post the screenshot of dataset fetch XML query and XML error.

    Thanks!

    Santa

  • Ramakanta Profile Picture
    2,715 on at

    My Query

    =======

    string acc = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' aggregate='true'>

                                    <entity name='account'>

                                      <attribute name='mi_totalamountdue' alias='sumoftotalcharges' aggregate='sum' />

                                      <attribute name='accountid' alias='accountid' groupby='true' />

                                    </entity>

                                  </fetch>";

    =====================================

    paging cookie Method(Get It From MSDN)

    ================================

    public EntityCollection GetAllRecords(string fetchXml, IOrganizationService orgService)

          {

              try

              {

                  int fetchCount = 5000;

                  // Initialize the page number.

                  int pageNumber = 1;

                  // Specify the current paging cookie. For retrieving the first page,

                  // pagingCookie should be null.

                  string pagingCookie = null;

                  while (true)

                  {

                      // Build fetchXml string with the placeholders.

                      string xml = CreateXml(fetchXml, pagingCookie, pageNumber, fetchCount);

                      // Excute the fetch query and get the xml result.

                      RetrieveMultipleRequest fetchRequest1 = new RetrieveMultipleRequest

                      {

                          Query = new FetchExpression(xml)

                      };

                      EntityCollection returnCollection = ((RetrieveMultipleResponse)orgService.Execute(fetchRequest1)).EntityCollection;

                      foreach (var c in returnCollection.Entities)

                      {

                          recordsList.Entities.Add(c);

                      }

                      // Check for morerecords, if it returns 1.

                      if (returnCollection.MoreRecords)

                      {

                          // Increment the page number to retrieve the next page.

                          pageNumber++;

                          // Set the paging cookie to the paging cookie returned from current results.                            

                          pagingCookie = returnCollection.PagingCookie;

                      }

                      else

                      {

                          // If no more records in the result nodes, exit the loop.

                          break;

                      }

                  }

                  return recordsList;

              }

              // Catch any service fault exceptions that Microsoft Dynamics CRM throws.

              catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>)

              {

                  // You can handle an exception here or pass it back to the calling method.

                  throw;

              }

                  return recordsList;

          }

          public string CreateXml(string xml, string cookie, int page, int count)

          {

              StringReader stringReader = new StringReader(xml);

              XmlTextReader reader = new XmlTextReader(stringReader);

              // Load document

              XmlDocument doc = new XmlDocument();

              doc.Load(reader);

              return CreateXml(doc, cookie, page, count);

          }

          public string CreateXml(XmlDocument doc, string cookie, int page, int count)

          {

              XmlAttributeCollection attrs = doc.DocumentElement.Attributes;

              if (cookie != null)

              {

                  XmlAttribute pagingAttr = doc.CreateAttribute("paging-cookie");

                  pagingAttr.Value = cookie;

                  attrs.Append(pagingAttr);

              }

              XmlAttribute pageAttr = doc.CreateAttribute("page");

              pageAttr.Value = System.Convert.ToString(page);

              attrs.Append(pageAttr);

              XmlAttribute countAttr = doc.CreateAttribute("count");

              countAttr.Value = System.Convert.ToString(count);

              attrs.Append(countAttr);

              StringBuilder sb = new StringBuilder(1024);

              StringWriter stringWriter = new StringWriter(sb);

              XmlTextWriter writer = new XmlTextWriter(stringWriter);

              doc.WriteTo(writer);

              writer.Close();

              return sb.ToString();

          }

  • Community Member Profile Picture
    on at

    use this one, I think you gave a space between count= '50000'. If we give any white space in XML string then it will show the invalid xml error.

    string acc = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' count='50000' aggregate='true'>

    Other than that, Are you using fetch XML query with Plugin?

    Thanks!

    Santa

  • Ramakanta Profile Picture
    2,715 on at

    Getting the same result i.e. 5000 records only

    Fetch.png

  • ANISH Profile Picture
    95 on at

    Thats true, FetchXML using aggregate does not return the records more than 5K, facing the same.

    I am not sure what is the alternate .

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans