在新窗口中打开的Web资源中使用Xrm.WebApi方法 [英] Using Xrm.WebApi method in Web Resource opened in a new window

查看:297
本文介绍了在新窗口中打开的Web资源中使用Xrm.WebApi方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用以下方法在新窗口中打开了HTML Web资源:

I have opened an HTML web resource in a new window using:

Xrm.Navigation.openWebResource(webResource,windowOptions,data);

这是HTML Web资源,它正在将ClientObject加载到头部

This is an HTML web resource and it is loading the ClientObject in the head

<脚本类型= text / javascript src = ../../../ ClientGlobalContext.js.aspx>< / script>

然后我有一些JavaScript正在尝试检索 Contact

then I have some JavaScript that is trying to retrieve a Contact

var contactId = "8553DA63-11C9-E711-A824-000D3AE0CB84";
var promise = Xrm.WebApi.retrieveRecord("contact", contactId, "$select=contactid,firstname,lastname");`

,但这是失败。我已逐步跟踪到 Xrm.WebApi 方法,发现错误是当它尝试解决联系人 设置为设置名称

but this is failing. I've step-traced into the Xrm.WebApi method and found the error is when it attempts to resolve "contact" to a Set Name


来自 Global.ashx的代码

getEntitySetName: function(logicalName) {
    Mscrm.Utilities.addTelemetryLog("Xrm.Utility.getEntitySetName");
    var $v_0 = window.ENTITY_SET_NAMES || window.top.ENTITY_SET_NAMES;
    if (IsNull(this.$5H_1) && !isNullOrEmptyString($v_0))
        this.$5H_1 = JSON.parse($v_0);
    return this.$5H_1[logicalName.toLowerCase()]
},


由于某些原因, window.ENTITY_SET_NAMES 对象为null,因此发生错误(空引用)

For some reason the window.ENTITY_SET_NAMES object is null so an error (null reference) occurs

我尝试将Web资源嵌入到CRM页面中,并且代码可以正常工作。问题似乎是通过 Xrm.Navigation.openWebResource

I've tried embedding my web resource into a CRM page and the code works correctly. The issue seems to be when the web resource is launched via Xrm.Navigation.openWebResource

启动Web资源时是否有人尝试使用 Xrm.WebApi 在使用 Xrm.Navigation.openWebResource 打开的Web资源的上下文中?还是有人知道是否需要采取其他步骤来检索数据?

Has anyone tried to use Xrm.WebApi in the context of an web resource opened with Xrm.Navigation.openWebResource? or does anyone know if there are additional steps required to retrieve data?

更新

ENTITY_SET_NAMES main.aspx 中初始化。
我尝试将自定义Web资源直接嵌入到新的 Main Form 部分中,并且 retrieveRecord 方法有效。

ENTITY_SET_NAMES is initialised in main.aspx. I tried embedding my custom Web Resource directly into a new Main Form section and the retrieveRecord method works.

仅当通过 Xrm.Navigation.openWebResource 从新页面运行Web资源时,这才出现问题。

It appears this is a problem only when running the Web Resource from a new page via Xrm.Navigation.openWebResource

更新2-对Aron的响应

我尝试按照以下建议使用 window.parent

I tried using window.parent as suggested below

var contactId = "8553DA63-11C9-E711-A824-000D3AE0CB84";
var promise = parent.Xrm.WebApi.retrieveRecord("contact", contactId, "$select=contactid,firstname,lastname");`

并且还尝试了 window.parent.top

var contactId = "8553DA63-11C9-E711-A824-000D3AE0CB84";
var promise = parent.top.Xrm.WebApi.retrieveRecord("contact", contactId, "$select=contactid,firstname,lastname");`

但都导致相同的错误

推荐答案

我的博客: )

为实现此目的,我实施了 hacky 解决方法。

To get this working I have implemented a hacky work-around.

我一直在调试Xrm.WebApi方法,并且在尝试使用实体名称并将其解析为 setname (复数)。它通过比较传递给 retrieveRecord 方法的值并将其与全局变量 ENTITY_SET_NAMES

I've been debugging the Xrm.WebApi method and it is failing on a line where it attempts to take the entityname and resolve it to the setname (plural). It does this by comparing the value passed into the retrieveRecord method and comparing it to a global variable ENTITY_SET_NAMES


在我的示例中,它试图将 contact 解析为 contacts

不幸的是,此变量不存在,并且 Xrm.WebApi 引发错误

This variable is unfortunately not present and Xrm.WebApi throws an error

我的解决方法是检查此变量,如果不存在,请创建它! ENTITY_SET_NAMES 是JSON可分析的字符串,其中包含每个实体的逻辑名和集名。

My work-around is to check for this variable, and if it is not present then create it! ENTITY_SET_NAMES is a JSON-parsable string which contains the logical name and set name for each entity.

window["ENTITY_SET_NAMES"] = window["ENTITY_SET_NAMES"] || JSON.stringify({
  "account" : "accounts",
  "contact" : "contacts"
});

在调用 Xrm.WebApi 方法似乎有效,我现在得到结果

Executing this line before any calls to Xrm.WebApi methods appears to work and I now get results

以下是完整的代码段:

window["ENTITY_SET_NAMES"] = window["ENTITY_SET_NAMES"] || JSON.stringify({
  "account" : "accounts",
  "contact" : "contacts"
});

var contactId = "8553DA63-11C9-E711-A824-000D3AE0CB84";
Xrm.WebApi.retrieveRecord(
  "contact", 
  contactId, 
  "$select=contactid,firstname,lastname"
).then(
  function success(result) {
    console.log(result.firstname);
    // perform operations on record retrieval
  },
  function (error) {
    console.log(error.message);
    // handle error conditions
  }
);

这篇关于在新窗口中打开的Web资源中使用Xrm.WebApi方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆