簡體   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