Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Page large result sets with FetchXML - for aggregate attribute

Posted on by Microsoft Employee

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

  • ANISH Profile Picture
    ANISH 95 on at
    RE: Page large result sets with FetchXML - for aggregate attribute

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

    I am not sure what is the alternate .

  • Ramakanta Profile Picture
    Ramakanta 2,715 on at
    RE: Page large result sets with FetchXML - for aggregate attribute

    Getting the same result i.e. 5000 records only

    Fetch.png

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Page large result sets with FetchXML - for aggregate attribute

    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
    Ramakanta 2,715 on at
    RE: Page large result sets with FetchXML - for aggregate attribute

    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
    Community Member Microsoft Employee on at
    RE: Page large result sets with FetchXML - for aggregate attribute

    Hi RK,

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

    Thanks!

    Santa

  • Ramakanta Profile Picture
    Ramakanta 2,715 on at
    RE: Page large result sets with FetchXML - for aggregate attribute

    Its giving "Invalid XML" error

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Page large result sets with FetchXML - for aggregate attribute

    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
    Ramakanta 2,715 on at
    RE: Page large result sets with FetchXML - for aggregate attribute

    Hi Radu,

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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans