MS Dynamics REST API FetchXML with a paging cookie

本文关键字:paging cookie with FetchXML Dynamics REST API MS | 更新日期: 2023-09-27 18:08:05

我正在使用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在这个工具中工作得很好,但不适合纯REST API请求。也许我对URL的编码或结构有一些问题。

MS Dynamics REST API FetchXML with a paging cookie

我遇到了同样的问题,我的解决方案是在分页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>

希望有帮助

对于这个问题,如何使用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是定义为

的regexp。

让pageCookieMatch =/(? & lt; = pagingcookie =[’])(a - z、a - z 0 - 9, % _。~)+/g;

我不知道为什么,但出于某种原因,您必须在使用相应的xml表示替换<>"之前明智地解码cookie。