简体   繁体   English

如何在带有C#插件的Dynamics CRM组织中使用Dynamics 365 Web API检索所有记录(超过5000页和/或一页以上)?

[英]How can I retrieve all records (more than 5000 and/or more than 1 page) with the Dynamics 365 Web API in a Dynamics CRM organization with a C# plugin?

I need to count all of the Task records with a regardingobjectid of a specific Account and a statecode of 0 . 我需要一个特定账户regardingobjectid0 statecode计算所有的任务记录。 This needs to be done with the new 365 Web API and REST that came with Microsoft Dynamics CRM 2016. Here is how the basic query (without paging) is done with Javascript: 这需要使用Microsoft Dynamics CRM 2016附带的新365 Web API和REST来完成。这是使用Javascript完成基本查询(不进行分页)的方式:

var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/tasks?$filter=_regardingobjectid_value eq 00000000-0000-0000-0000-000000000000 and  statecode eq 0", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\",odata.maxpagesize=5000");
req.onreadystatechange = function() {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
            var results = JSON.parse(this.response);
            for (var i = 0; i < results.value.length; i++) {
                var activityid = results.value[i]["activityid"];
            }
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send();

Furthermore, this must be done in a fashion that allows it to retrieve more than 5000 records. 此外,必须以允许其检索超过5000条记录的方式来完成此操作。 Information on how to do this can be found here: https://msdn.microsoft.com/en-us/library/gg334767.aspx 有关如何执行此操作的信息,可以在这里找到: https : //msdn.microsoft.com/zh-cn/library/gg334767.aspx

Here is how I could query up to 5000 records with a C# plugin and FETCH XML: 这是我可以使用C#插件和FETCH XML查询多达5000条记录的方法:

int count = 0;
            string fetchTemplate =
                @"<fetch version='1.0' output-format='xml - platform' mapping='logical' distinct='false'>
                    <entity name='activitypointer'>
                        <attribute name='activitytypecode' />
                        <attribute name='subject' />
                        <attribute name='prioritycode' />
                        <attribute name='regardingobjectid' />
                        <attribute name='statecode' />
                        <attribute name='instancetypecode' />
                        <attribute name='community' />
                        <attribute name='scheduledend' />
                        <attribute name='activityid' />
                        <filter type='and'>
                            <condition attribute='isregularactivity' operator='eq' value='1' />
                            <condition attribute='statecode' operator='eq' value='0' />
                        </filter>    
                        <link-entity name='account' from='accountid' to='regardingobjectid' alias='ab'>
                            <filter type='and'>
                                <condition attribute='accountid' operator='eq' uitype='account' value='{0}' />
                            </filter>
                        </link-entity>
                    </entity>
                </fetch>";
            fetchTemplate = String.Format(fetchTemplate, entityId.ToString());
            List<Entity> records = context.RetrieveAll(fetchTemplate);
            if (records != null && records.Count > 0)
            {
                count = records.Count;
            }

Any help on this would be greatly appreciated. 任何帮助,将不胜感激。 I am using Microsoft Dynamics CRM Online 2016, Visual Studio Professional 2015, relevant Nuget packages, and the Plugin Registration Tool. 我正在使用Microsoft Dynamics CRM Online 2016,Visual Studio Professional 2015,相关的Nuget程序包和插件注册工具。

I have come to StackOverflow for help with this because I cannot find a simple example of a REST query online for Microsoft Dynamics CRM that can retrieve more than 5000 records in a C# plugin. 我之所以向StackOverflow寻求帮助,是因为我找不到在线的Microsoft Dynamics CRM REST查询的简单示例,该示例可以在C#插件中检索5000多个记录。

I am particularly interested in the @odata.nextlink property and how this enables the restful retrieval of multiple pages (and potentially more than 5000 records). 我对@ odata.nextlink属性以及它如何使多个页面(可能超过5000条记录)的平稳检索特别感兴趣。

If you would like to help me enhance my current C# plugin code so that it can retrieve more than 5000 records, that would also be appreciated. 如果您想帮助我增强当前的C#插件代码,使其可以检索超过5000条记录,也将不胜感激。

You don't need to retrieve all the records to perform a count. 您无需检索所有记录即可进行计数。 FetchXML includes aggregations that allow us to calculate max, min, average and COUNT . FetchXML包含的聚合使我们能够计算最大,最小,平均值和COUNT The following is an example to count all the accounts in the system: 以下是计算系统中所有帐户的示例:

<fetch version="1.0" mapping="logical" aggregate="true">
  <entity name="account">
    <attribute name="accountid" aggregate="count" alias="count" />
  </entity>
</fetch> 

The WebAPI have a $count query option, but unfortunately, it's also limited by the same restriction so it's not helpful in this case: WebAPI具有$ count查询选项,但不幸的是,它也受到相同限制的限制,因此在这种情况下它没有帮助:

The count value does not represent the total number of entities in the system. 计数值不代表系统中实体的总数。 It is limited by the maximum number of entities that can be returned. 它受可以返回的最大实体数限制。

So, after all this introduction, how we calculate the number of records using the WebAPI and avoiding the 5000 records restriction then? 那么,在完成所有这些介绍之后,我们如何使用WebAPI计算记录数并避免5000条记录的限制呢? Passing our FetchXML as a query in the GET to the WebAPI, this is how to use it. 将FetchXML作为GET中的查询传递给WebAPI, 就是使用它的方法。 After encoding the previous FetchXML example, we ended up with the following request: 在对前面的FetchXML示例进行编码之后,我们得到了以下请求:

GET/api/data/v8.2/accounts?fetchXml=%3Cfetch+version%3D%221.0%22+mapping%3D%22logical%22+aggregate%3D%22true%22%3E%3Centity+name%3D%22account%22%3E%3Cattribute+name%3D%22accountid%22+aggregate%3D%22count%22+alias%3D%22count%22+%2F%3E%3C%2Fentity%3E%3C%2Ffetch%3E+

And this is the response with our count: 这是我们的回应: 在此处输入图片说明

If you still want to retrieve all the records, here you can find an example of how to use the paging cookie with the WebAPI. 如果仍要检索所有记录,则可以在此处找到有关如何将分页cookie与WebAPI一起使用的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM