FetchAll() for Microsoft Dynamics CRM 2011 – Handling Large FetchXML Resultsets

When I originally started working in Microsoft Dynamics CRM 2011, I was convinced that I would be able to avoid FetchXML forever, in favor of all the new shiny LINQ / REST capabilities. Well, it turns out I was wrong, and we are still finding places for FetchXML, which means I eventually ran into an issue where we needed to update our old ‘FetchAll()’ utility method from Microsoft Dynamics CRM 4.0.

As you probably already know, there is a maximum resultset size (5000) in CRM for Fetch queries. You /can/ disable this via a registry setting, but that’s not always feasible (most notably in Microsoft Dynamics CRM Online). Back in the ‘old days’, several blogs presented solutions for using the paging/morerecords attributes of the fetch result, which looped until no more pages were found. We used this with great success with some large integration projects where we had to move large numbers of records around.

In Microsoft Dynamics CRM 2011 the mechanism for invoking Fetch has changed slightly, meaning the previous 4.0 FetchAll() needs some minor updating. Without further ado, here it is. Some of this code may look familiar because it originated from one of those aforementioned blogs.

*Fetch method – this is called by FetchAll to get each successive page.

*Depending on how you structure your code and server connections, service.OrgServiceProxy below will just be whatever your Org Service object is.

*FetchAll method – this checks for a ‘page’ attribute in the fetchXML – if there is not one, it sets it. After each Fetch, it appends the entities to the ‘allRecords’ EntityCollection and increments the Page counter until morerecords = false.

*It might be a little nicer to just have an EntityCollection variable for allRecords instead of a RetrieveMultipleResponse, but at this point I decided to maintain a consistent return type between a regular Fetch and FetchAll. Some of the fields on allRecords (like the ‘morerecords’ and ‘paging’ data are obviously leftover from the first page of results, but those fields are irrelevant with a FetchAll result anyway.)

2 thoughts on “FetchAll() for Microsoft Dynamics CRM 2011 – Handling Large FetchXML Resultsets”

  1. Sorting the unified result set is not really a CRM question but a more general .NET question.

    Now that you have a single EntityCollection with an aggregated Entities property, you should be able to use standard .NET sorting approaches for collections. Here are a couple of references that might get you on the right track.

    http://www.blayd.co.uk/article.aspx?pageid=1017#sectionSix (especially 'Sorting with Extension Methods') http://stackoverflow.com/questions/802002/fluent-sorting-of-a-listt-on-multiple-criteria-using-extension-methods

  2. Would it be possile in this way to implement a sorting strategy by multiple criteria? I mean something like a ByNameAndDate strategy?Could you give us an example?

Comments are closed.

Show Buttons
Hide Buttons