How to fetch more than 5000 entities from CRM

前端 未结 3 1053
你的背包
你的背包 2021-01-04 21:48

I\'m querying MS Dynamics CRM Online from my console app:

public EntityCollection GetEntities(string entityName)
{
    IOrganizationService proxy = ServerCon         


        
3条回答
  •  伪装坚强ぢ
    2021-01-04 22:06

    You can use LINQ as shown below. The CRM LINQ provider will automatically page the query and run as many requests as is needed to get the full result, and return the complete set in a single object. It's really convenient for us. However, be careful with that. If the result set is very large it will be noticeably slow, and it could even throw an OutOfMemoryException in extreme cases.

     public List 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 like much better than the official examples:

    int page = 1;
    EntityCollection entityList = new EntityCollection();
    
    do
    {
        entityList = Context.RetrieveMultiple(new FetchExpression(String.Format(" ... ", SecurityElement.Escape(entityList.PagingCookie), page++)));
    
        // Do something with the results here
    }
    while (entityList.MoreRecords);
    

提交回复
热议问题