首页 > 解决方案 > FetchXml-Request 不起作用,如果它包含“页面”(无效的 XML)

问题描述

我正在为 Dynamics CRM (8.2) 构建一个网络资源。当我尝试像这样使用 fetchXml 获取数据时,一切都很好:

<fetch mapping="logical" count="3" >
    <entity name="product" >
        <attribute name="name" />
        <attribute name="productnumber" />
    </entity>
</fetch>

但是如果我添加“页面”参数,就像在下一个代码中一样,我会得到无效的 XML。

<fetch mapping="logical" count="3" page="1" >
    <entity name="product" >
        <attribute name="name" />
        <attribute name="productnumber" />
    </entity>
</fetch>

我使用 XrmToolBox 中的 FetchXml 测试器测试了获取。在那里它工作正常。我做错什么了?

我尝试使用 npm 包 dynamics-web-api ( https://www.npmjs.com/package/dynamics-web-api ) 和 XMLHttpRequest 来执行请求。两者都像上面描述的那样工作。

错误消息是这样的:(抱歉格式化)

{
  "error":{
    "code":"",
    "message":"Invalid XML.",
    "innererror":{
      "message":"Invalid XML.",
      "type":"System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]",
      "stacktrace":"   at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.RetrieveMultiple(QueryBase query, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType, Boolean checkAdminMode)
         at Microsoft.Crm.Extensibility.OData.CrmODataExecutionContext.RetrieveMultiple(QueryBase query)
         at Microsoft.Crm.Extensibility.OData.CrmODataServiceDataProvider.ExecuteQueryForEntitySet(CrmODataExecutionContext context, String entitySetName, CustomQueryOptions queryOptions, String fetchXml)
         at Microsoft.Crm.Extensibility.OData.CrmODataServiceDataProvider.RetrieveEdmEntityCollection(CrmODataExecutionContext context, String entityCollectionName, String castedEntityName, ODataQueryOptions queryOptions)
         at Microsoft.Crm.Extensibility.OData.EntityController.GetEntitySet(String entitySetName)
         at lambda_method(Closure , Object , Object[] )
         at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
         at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
         --- End of stack trace from previous location where exception was thrown ---
         at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
         at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
         at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()
         --- End of stack trace from previous location where exception was thrown ---
         at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
         at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
         at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()
         --- End of stack trace from previous location where exception was thrown ---
         at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
         at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
         at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"
    }
  }
  }

**Edit**
My error were somewhere in the formatting of the fetch-string. I used:

常量查询 = encodeURI(connectionString + "?fetchXml=" + encodeURI(xmlString));

the correct code is:

常量查询 = encodeURI(connectionString) + "?fetchXml=" + encodeURI(xmlString);


Ty AnkUser for your example. It helped me much.

标签: xmldynamics-crmcrmfetchxmlxrm

解决方案


我刚刚尝试使用Page=1我的以下请求和 Fetchxml,它对我来说效果很好注意我在我的示例中使用了 Account,但这应该不是问题。

<fetch mapping="logical" count="3" page="1" >
        <entity name="account" >
            <attribute name="name" />
            <attribute name="accountnumber" />
        </entity>

var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/accounts?fetchXml=%3Cfetch%20mapping%3D%22logical%22%20count%3D%223%22%20page%3D%221%22%20%3E%3Centity%20name%3D%22account%22%20%3E%3Cattribute%20name%3D%22name%22%20%2F%3E%3Cattribute%20name%3D%22accountnumber%22%20%2F%3E%3C%2Fentity%3E%3C%2Ffetch%3E", false);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function() {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
            var results = JSON.parse(this.response);
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send();

推荐阅读