Dynamic CRM Web API 8.2“分页Cookie中格式错误的XML”

时间:2019-02-12 15:38:07

标签: javascript xml dynamics-crm

我使用Web资源JavaScript文件从CRM检索多条记录。

var fetchXML = `
            <fetch mapping="logical" output-format="xml-platform" version="1.0" page="1">
              <entity name="account" >
                <attribute name="name" />
              </entity>
            </fetch>`;

var query = "accounts?fetchXml=" + fetchXML;

callWebAPI(query);

在第一个请求中获得分页cookie后,我尝试将其发送到第二个请求以检索第二个页面的数据:

<fetch mapping="logical" output-format="xml-platform" version="1.0" page="2" paging-cookie="cookie i get from first request"
     ...
</fetch>`;

响应中的原始Cookie如下:

%253ccookie%2520page%253d%25221%2522%253e%253cname%2520last%253d%2522Deco%2520Voyages%2522%2520firstnull%253d%25221%2522%2520%252f%253e%253caccountid%2520last%253d%2522%257b9AFBEAA6-9EA7-E711-8103-70106FAA4841%257d%2522%2520first%253d%2522%257b0A86656D-BEA7-E711-8103-70106FAA4841%257d%2522%2520%252f%253e%253c%252fcookie%253e

我尝试根据文档https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/org-service/page-large-result-sets-with-fetchxml

来转换和发送cookie
var transformedCookie1 = GetDecodedCookie1(decodeURIComponent(decodeURIComponent(pagingcookie)));

var transformedCookie2 = GetDecodedCookie2(decodeURIComponent(decodeURIComponent(pagingcookie)));

function GetDecodedCookie1(cookie) {
    return cookie.replace(/</g, "&lt;")
                 .replace(/>/g, "&gt;")
                 .replace(/"/g, "&quot;")
}

function GetDecodedCookie2(cookie) {
    return cookie.replace(/</g, "%26lt;")
                 .replace(/>/g, "%26gt;")
                 .replace(/"/g, "%26quot;")
}

1)在第一种情况下,当我使用GetDecodedCookie1时,我得到:

Script error. in  at 0:0  null

enter image description here

我的查询字符串参数已损坏。

2)在第二种情况下,当我使用GetDecodedCookie1时,查询字符串参数看起来不错,但是我得到了:

Malformed XML in the Paging Cookie

这是什么问题?

2 个答案:

答案 0 :(得分:0)

分页Cookie需要最后一条记录的ID,包括“帐户ID”作为fetchxml的属性

希望有帮助-M.Acosta.D

答案 1 :(得分:0)

是否必须使用FetchXML来查询Web API?否则,您可以使用OData简化生活。转换为OData查询的FetchXML为:

https://[Organization URI]/api/data/v8.2/accounts?$select=name

如果Dynamics中将有5000个以上的帐户,则返回的响应类似于下面的响应,否则将不包含@ odata.nextLink属性(我折叠了值集合以提高可读性):

{
     "@odata.context": "https://[Organization URI]/api/data/v8.2/$metadata#accounts(name)",
     "value": [],
     "@odata.nextLink": "https://[Organization URI]/api/data/v8.2/accounts?$select=name&$skiptoken=%3Ccookie%20pagenumber=%222%22%20pagingcookie=%22%253ccookie%2520page%253d%25221%2522%253e%253caccountid%2520last%253d%2522%257b92655027-054C-E911-A817-000D3ABA3F3F%257d%2522%2520first%253d%2522%257b93C71621-BD9F-E711-8122-000D3A2BA2EA%257d%2522%2520%252f%253e%253c%252fcookie%253e%22%20istracking=%22False%22%20/%3E"
}

检索到此响应后,您只需解析它,然后对@ odata.nextLink属性给出的链接进行请求以检索下一批记录,就我而言,此链接将检索帐户5001到10000。

有关如何使用Web API查询数据的更多信息,请单击here获取官方文档。