繁体   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?

我需要一个特定账户regardingobjectid0 statecode计算所有的任务记录。 这需要使用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/zh-cn/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条记录,也将不胜感激。

您无需检索所有记录即可进行计数。 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, 就是使用它的方法。 在对前面的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+

这是我们的回应: 在此处输入图片说明

如果仍要检索所有记录,则可以在此处找到有关如何将分页cookie与WebAPI一起使用的示例。

暂无
暂无

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

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