C# 如何从CRM获取5000多个实体

C# 如何从CRM获取5000多个实体,c#,dynamics-crm-2013,dynamics-crm-online,C#,Dynamics Crm 2013,Dynamics Crm Online,我正在从我的控制台应用程序查询MS Dynamics CRM Online: public EntityCollection GetEntities(string entityName) { IOrganizationService proxy = ServerConnection.GetOrganizationProxy(); string request = string.Format("<fetch mapping ='logical'><entity

我正在从我的控制台应用程序查询MS Dynamics CRM Online:

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;
}
公共EntityCollection GetEntities(字符串entityName) { IOOrganizationService proxy=ServerConnection.GetOrganizationProxy(); 字符串请求=string.Format(“,entityName”); FetchExpression=新的FetchExpression(请求); var mult=proxy.RetrieveMultiple(表达式); 返回mult; } 此代码在
mult.Entities
中最多只返回5000个元素。我知道CRM中有更多的实体。
如何检索所有实体?

使用fetch XML一次只能检索5000条记录

要获取更多记录,必须使用分页cookie,请参见此处:

相关的代码位:

// 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;
修改了主循环,因为示例似乎没有更新分页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;
    }
}

我个人倾向于使用LINQ而不是FetchXML,但值得注意的是Lasse V.Karlsen所说的,如果您向用户呈现此信息,您可能希望进行某种分页(使用FetchXML或LINQ)

您可以使用LINQ,如下所示。CRM LINQ提供程序将自动分页查询,并根据需要运行尽可能多的请求以获得完整结果,并在单个对象中返回完整集。这对我们来说真的很方便。但是,要小心。如果结果集非常大,那么速度会非常慢,在极端情况下甚至可能抛出OutOfMemoryException

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

        return DataContext.CreateQuery(entityName).toList();
    }
公共列表GetEntities(字符串entityName) { OrganizationServiceContext DataContext=新的OrganizationServiceContext(ServerConnection.GetOrganizationProxy()); 返回DataContext.CreateQuery(entityName.toList(); } 下面是一个使用FetchXML分页的替代实现,与官方示例相比,我更喜欢它:

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);
int page=1;
EntityCollection entityList=新EntityCollection();
做
{
entityList=Context.RetrieveMultiple(新的FetchExpression(String.Format(“…”,SecurityElement.Escape(entityList.PagingCookie),page++));
//在这里对结果做点什么
}
while(entityList.morecords);

我也有同样的问题。我通过在fetchxml或查询表达式中包含实体的id来解决这个问题。如果在查询中包含传入“lead”,则还应添加“leadid”。然后自动生成分页cookie。

为什么要查询这么多记录?当然,您不会向用户显示超过5000行以供查看/选择?当您希望以编程方式将数据从CRM下载到自定义应用程序的数据库中时,这种情况是可能的。本例中的代码的pagingCookie=null。在结果的第二页,此代码将抛出OrganizationServiceFault异常“尝试检索任何高页面上的一组记录时需要分页cookie”。MSDN示例似乎不会更新分页cookie。它应该作为从RetrieveMultipleThat返回的结果的属性可用。如果您有一个包含多个页面的resultset,则此代码将起作用。如果结果中的可用记录多于第一页,则if(result.morecords)将为true,然后您将设置分页cookie。如果您从这段代码中得到一个错误,请尝试单步调试,看看为什么您没有得到分页cookie。上周我自己使用了这个例子,它对我很有效,当然你需要一个合理的结果集。祝你好运虽然(正确){…}通常被认为是一种不好的做法。是的,我知道这是sdk中的官方示例。PagingCookie属性设置为NULL,因为必须包含查询实体的ID属性。我不认为您可以分享这样的示例,可以吗?我试图实现它,但我迷路了。