【问题标题】: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?如何在具有 C# 插件的 Dynamics CRM 组织中使用 Dynamics 365 Web API 检索所有记录(超过 5000 条和/或超过 1 页)?
【发布时间】:2017-05-31 20:08:18
【问题描述】:

我需要用特定 Account 的 regardingobjectid 和 statecode 计算所有 Task 记录0。这需要使用 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();

此外,这必须以允许检索超过 5000 条记录的方式完成。可以在此处找到有关如何执行此操作的信息:https://msdn.microsoft.com/en-us/library/gg334767.aspx

以下是我如何使用 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;
            }

对此的任何帮助将不胜感激。我正在使用 Microsoft Dynamics CRM Online 2016、Visual Studio Professional 2015、相关的 Nuget 包和插件注册工具。

我来 StackOverflow 寻求帮助,因为我找不到一个简单的 Microsoft Dynamics CRM 在线 REST 查询示例,它可以在 C# 插件中检索超过 5000 条记录。

我对@odata.nextlink 属性以及它如何实现对多个页面(以及可能超过 5000 条记录)的宁静检索特别感兴趣。

如果您愿意帮助我增强我当前的 C# 插件代码,使其可以检索超过 5000 条记录,我们将不胜感激。

【问题讨论】:

  • 从您发布的链接中:a maximum of 5000 entities will be returned for each request. If there are more entities that match the query filter criteria, a @odata.nextLink property will be returned with the results. Use the value of the @odata.nextLink property with a new GET request to return the next page of data. 我的 C# 相当弱,但 @odata.nextLink 应该返回一个值,您可以使用该值再次为接下来的 5000 条记录调用 API。跨度>
  • 其次,这会更容易通过CRM中的报告吗?见此链接:powerobjects.com/2015/11/09/determining-total-record-counts
  • 感谢您的宝贵时间和 cmets。现在,如果我能用 C# 和 REST 做一个简单的查询就好了……我明天会继续努力。
  • 不幸的是,使用报告不会有任何好处。获得计数后,我需要使用该计数更新另一条记录中的自定义字段。老实说,我认为这仍然可以通过汇总或计算字段来完成。但是,我使用 REST 来做这件事是为了精通它和插件开发。

标签: c# .net rest dynamics-crm dynamics-crm-365


【解决方案1】:

您无需检索所有记录即可执行计数。 FetchXML 包含允许我们计算最大值、最小值、平均值和COUNT 的聚合。以下是统计系统中所有账户的示例:

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

WebAPI 有一个 $count 查询选项,但不幸的是,它也受到相同限制,因此在这种情况下没有帮助:

计数值不代表实体的总数 系统。它受最大实体数量的限制 返回。

那么,经过这么多的介绍,我们如何使用 WebAPI 计算记录数并避免 5000 条记录的限制呢?将我们的 FetchXML 作为 GET 中的查询传递给 WebAPI,this 是如何使用它的。在对前面的 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+

这是我们计数的响应:

如果您仍想检索所有记录,here 您可以找到如何将分页 cookie 与 WebAPI 一起使用的示例。

【讨论】:

  • 当您说“将我们的 FetchXML 作为 GET 中的查询传递给 WebAPI,这就是如何使用它。”我仍然很困惑。您链接到的页面中没有 C# 代码。您能否提供示例 C# 代码使用: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+
  • Here 你可以看到如何使用 C# 中的 WebAPI 的示例(也有一些代码来处理身份验证)。让我说,如果您想从插件执行此操作,您应该使用插件上下文中提供的组织服务(仍然使用 WCF 端点),而不是像那样使用 WebAPI 端点。
猜你喜欢
  • 1970-01-01
  • 2017-07-05
  • 1970-01-01
  • 2018-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多