【发布时间】: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