【问题标题】:MS Dynamics REST API FetchXML with a paging cookie带有分页 cookie 的 MS Dynamics REST API FetchXML
【发布时间】:2017-01-28 08:41:30
【问题描述】:

我正在使用MSDN 提供的这种方法来检索超过 5000 条记录。我将 MS Dynamics REST API 与 fetchXML 一起使用。我的第一个请求如下所示:

//ROOT.dynamics.com//api/data/v8.1/contacts?fetchXml=<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false"><entity name="contact"><attribute name="emailaddress1" /></entity></fetch>

结果我得到了一组记录和下一页的页面 cookie:

"@Microsoft.Dynamics.CRM.fetchxmlpagingcookie":"<cookie pagenumber=\"2\" pagingcookie=\"%253ccookie%2520page%253d%25221%2522%253e%253ccontactid%2520last%253d%2522%257bAF17816D-3AAE-E511-80FC-1458D043A570%257d%2522%2520first%253d%2522%257bFF672EF5-22AE-E511-80FC-1458D043A570%257d%2522%2520%252f%253e%253c%252fcookie%253e\" istracking=\"False\" />"

我在下一个请求中使用这个 cookie:

//ROOT.dynamics.com//api/data/v8.1/contacts?fetchXml=<fetch mapping='logical' paging-cookie='&lt;cookie page="1"&gt;&lt;contactid last="{UUID}" first="UUID}"/&gt;&lt;/cookie&gt;' page='2'><entity name='contact'><all-attributes/></entity></fetch>

得到了

{
Message: "An item with the same key has already been added.",
ExceptionMessage: "An item with the same key has already been added.",
ExceptionType: "System.ArgumentException",
StackTrace: " at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector) at Microsoft.Crm.Extensibility.OData.CrmODataHeaders.ProcessVersioningQueryParameters() at Microsoft.Crm.Extensibility.OData.CrmODataHeaders..ctor(HttpRequestMessage request) at Microsoft.Crm.Extensibility.OData.Extensions.EdmExtensions.GetCrmODataRequestHeader(HttpRequestMessage request) at Microsoft.Crm.Extensibility.OData.CrmODataRoutingConvention.SelectController(ODataPath odataPath, HttpRequestMessage request) at System.Web.OData.Routing.ODataPathRouteConstraint.SelectControllerName(ODataPath path, HttpRequestMessage request) at System.Web.OData.Routing.ODataPathRouteConstraint.Match(HttpRequestMessage request, IHttpRoute route, String parameterName, IDictionary`2 values, HttpRouteDirection routeDirection) at System.Web.Http.Routing.HttpRoute.ProcessConstraint(HttpRequestMessage request, Object constraint, String parameterName, HttpRouteValueDictionary values, HttpRouteDirection routeDirection) at System.Web.Http.Routing.HttpRoute.ProcessConstraints(HttpRequestMessage request, HttpRouteValueDictionary values, HttpRouteDirection routeDirection) at System.Web.Http.Routing.HttpRoute.GetRouteData(String virtualPathRoot, HttpRequestMessage request) at System.Web.Http.WebHost.Routing.HttpWebRoute.GetRouteData(HttpContextBase httpContext)",
ErrorCode: 500
}

这个 fetchXML 在这个 tool 中运行良好,但不适用于纯 REST API 请求。 也许我对 URL 的编码或结构有一些问题。

【问题讨论】:

    标签: c# cookies dynamics-crm-2011 dynamics-crm crm


    【解决方案1】:

    我遇到了同样的问题,我的解决方案是对分页 cookie 中的 & 符号进行 uri 编码

    //ROOT.dynamics.com//api/data/v8.1/contacts?fetchXml=<fetch mapping='logical' paging-cookie='%26lt;cookie page=%26quot;1%26quot;%26gt;%26lt;contactid last=%26quot;{UUID}%26quot; first=%26quot;UUID}%26quot;/%26gt;%26lt;/cookie%26gt;' page='2'><entity name='contact'><all-attributes/></entity></fetch>
    

    希望有帮助

    【讨论】:

    • 非常感谢...终于成功了!我必须: 1.在 PostMan 中获取 cookie 2. 取消转义两次 3.URL-Encode 和 4. 将“&”替换为“%26”。现在我可以开始在 Node.js 中编写整个过程了。顺便说一句,我正在调用 WebAPI
    【解决方案2】:

    对于如何使用 fetchxml pagecookie 来获取下一页的问题,假设您有一个 json 对象响应,其中包含通过 webapi 进行的初始 fetchxml 调用,并且分页 cookie 设置为空白,我花了一些时间才终于让它工作:

    var fragment = "";
    var pageCookie = response["@Microsoft.Dynamics.CRM.fetchxmlpagingcookie"] as string;
    if (pageCookie != null && pageCookie != '') {
    
    
    fragment = decodeURIComponent(decodeURIComponent(pageCookie.match(pageCookieMatch)[0]));
    	fragment = fragment.replace(/&/g,'&amp;').replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\"/g, "&quot;");
    }
    
    
    let fetchxml = "<fetchxml mapping='logical' paging-cookie='"+fragment+"' page='"+ nextpage +"' count='"+pagesize+"'>...</fetchxml>";

    pageCookieMatch 是一个正则表达式,定义为

    让 pageCookieMatch = /(?

    我不知道为什么,但由于某种原因,您必须在将 " 替换为相应的 xml 表示之前对 cookie 进行两次解码。

    【讨论】:

      猜你喜欢
      • 2015-06-05
      • 1970-01-01
      • 2021-02-19
      • 1970-01-01
      • 2022-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多