How to get more than 5000 objects from CRM

I am querying MS Dynamics CRM Online from my console application:

public EntityCollection GetEntities(string entityName) { IOrganizationService proxy = ServerConnection.GetOrganizationProxy(); string request = string.Format("<fetch mapping ='logical'><entity name = '{0}'></entity></fetch>", entityName); FetchExpression expression = new FetchExpression(request); var mult = proxy.RetrieveMultiple(expression); return mult; } 

This code returns a maximum of 5000 elements in mult.Entities . I know that there are more objects in CRM. How to get all entites?

+7
source share
3 answers

You can only get 5,000 records at a time using an XML file.

To get more entries, you should use a paging cookie, see here:

Example: use FetchXML with a paging file

Corresponding code bits:

 // Define the fetch attributes. // Set the number of records per page to retrieve. int fetchCount = 3; // Initialize the page number. int pageNumber = 1; // Specify the current paging cookie. For retrieving the first page, // pagingCookie should be null. string pagingCookie = null; 

The main loop has been changed since the sample does not seem to update the swap cookie:

 while (true) { // Build fetchXml string with the placeholders. string xml = CreateXml(fetchXml, pagingCookie, pageNumber, fetchCount); FetchExpression expression = new FetchExpression(xml); var results = proxy.RetrieveMultiple(expression); // * Build up results here * // Check for morerecords, if it returns 1. if (results.MoreRecords) { // Increment the page number to retrieve the next page. pageNumber++; pagingCookie = results.PagingCookie; } else { // If no more records in the result nodes, exit the loop. break; } } 

I personally prefer to use LINQ rather than FetchXML, but it is worth noting that Lasse W. Carlsen said if you present this information to the user, you probably want to do some kind of paging (either in FetchXML or in LINQ)

+9
source

You can use LINQ as shown below. The CRM LINQ provider will automatically print the query and run as many queries as necessary to get the full result, and return the complete set to one object. It is very convenient for us. However, be careful with this. If the result set is very large, it will be noticeably slow, and it may even throw an OutOfMemoryException in extreme cases.

  public List<Entity> GetEntities(string entityName) { OrganizationServiceContext DataContext = new OrganizationServiceContext(ServerConnection.GetOrganizationProxy()); return DataContext.CreateQuery(entityName).toList(); } 

Here's an alternative implementation for paging with FetchXML, which I love much better than the official examples:

 int page = 1; EntityCollection entityList = new EntityCollection(); do { entityList = Context.RetrieveMultiple(new FetchExpression(String.Format("<fetch version='1.0' page='{1}' paging-cookie='{0}' count='1000' output-format='xml-platform' mapping='logical' distinct='false'> ... </fetch>", SecurityElement.Escape(entityList.PagingCookie), page++))); // Do something with the results here } while (entityList.MoreRecords); 
+5
source

I had the same problem. I solved this by including an object identifier in xetch or a query expression. If you include the passage in the "lead" in the query, then also add the "leadid". Then a paging cookie is automatically created for you.

0
source

Source: https://habr.com/ru/post/1206238/


All Articles