带有分页cookie的MS Dynamics REST API FetchXML

时间:2016-09-20 12:14:19

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

我正在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的编码或结构有一些问题。

2 个答案:

答案 0 :(得分:3)

我遇到了同样的问题,我的解决方法是在分页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 :(得分:0)

对于这个问题,如何使用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是一个正则表达式,定义为

let pageCookieMatch = /(?<=pagingcookie=['"])[a-z,A-Z,0-9,%-_.~]+/g;

我不确定为什么,但是由于某些原因,在将<>“替换为相应的xml表示形式之前,您必须先对cookie进行twise解码。