xml - 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.
解决方案
我刚刚尝试使用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();
推荐阅读
- maven-3 - 无法让 maven-assembly-plugin 打包测试
- jquery - jQuery 函数仅在我手动将窗口大小调整为移动设备而不是刷新时才有效
- java - 如何将项目输入资源放在 JAR 之外,如 Lib 文件夹
- python - TypeError 输出似乎倒退
- google-apps-script - Google 表格 - 用于将表格导出为仅包含值和格式的 xls 的按钮 - 不是公式
- rust - 如何从更高级别的特征绑定特征返回关联类型?
- r - 如何在不使用移位功能的情况下引用数据表(R)中的上一行?
- android - 如何在反应原生 TVEventHandler 中检测保持和快进事件
- python - 无法将numpy数组重塑为向量
- javascript - Javascript: Css-Selectors: (Bug or Feature?) Class-Name "starts-with" 只选择元素,当类在类列表中的第一个位置时