javascript 无法从 CRM 2011 中的 HTML Web 资源中访问 Xrm.Page.data

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21790176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 21:42:05  来源:igfitidea点击:

Cannot access Xrm.Page.data from within HTML web resource in CRM 2011

javascripthtmldynamics-crm-2011

提问by severalservals

I'm trying access the Xrm.Page.data object from within an HTML web resource that I have inserted onto a form in CRM 2011. However, depending on how I try to access the Xrm entity, I find that it is undefined or that Xrm.Page.data is null. The code for the web resource is as follows:

我正在尝试从插入到 CRM 2011 中的表单的 HTML Web 资源中访问 Xrm.Page.data 对象。但是,根据我尝试访问 Xrm 实体的方式,我发现它未定义或Xrm.Page.data 为空。web资源的代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script type="text/javascript">

function OpenMyApp(e){
    alert('Xrm defined: ' + (typeof Xrm != 'undefined'));
        // The line above returns the string 'Xrm defined: false'

    alert('window.top.opener.parent.Xrm defined: ' + (typeof window.top.opener.parent.Xrm != 'undefined'));
        // The line above returns the string 'window.top.opener.parent.Xrm defined: true'


    alert('frames[0].Xrm defined: ' + (typeof frames[0].Xrm != 'undefined'));
        // the line above will actually throw an error and stop the script, because the frames collection is empty. 

    alert(window.top.opener.parent.Xrm.Page.data);
        // the line above returns null. 

    // var myId = Xrm.Page.data.entity.attributes.get("new_field_i_want").getValue();
        // The line above is what I would like to see work. 

    e.preventDefault();
 }
</script>

</head>
<body>
<a onClick="OpenMyApp(event);" href="#">My Link</a>
</body>
</html>

I've accessed Xrm.Page.data successfully from within a JavaScript function that is part of a library that fires upon a form event (for instance, Form.Load). It's just when it's embedded in an HTML web resource on the form that I run into this problem. Can anyone explain what I'm doing wrong, and if there is actually a way to access Xrm.Page.data in that way that I would like to do?

我已经从 JavaScript 函数中成功访问了 Xrm.Page.data,该函数是触发表单事件(例如 Form.Load)的库的一部分。正是当它嵌入到表单上的 HTML Web 资源中时,我才遇到此问题。任何人都可以解释我做错了什么,如果实际上有一种方法可以以我想要的方式访问 Xrm.Page.data?

Thank you.

谢谢你。

回答by Andrew Butenko

Try to access Xrm using following syntax:

尝试使用以下语法访问 Xrm:

window.parent.Xrm.Page.getAttribute()...

window.parent.Xrm.Page.getControl()...

window.parent.Xrm.Page.context...

like

喜欢

alert(window.parent.Xrm.Page.data.entity.attributes.get("new_field_i_want").getValue());

From your sample code.

从您的示例代码。

回答by TWilly

This works for when you have a web resource that is loaded within an iframe/dialog. It gets access to the parent frame, then looks for all available frames, and checks which frame has

这适用于在 iframe/对话框中加载 Web 资源的情况。它可以访问父框架,然后查找所有可用的框架,并检查哪个框架具有

Xrm.Page.data != null

Xrm.Page.data != null

Code...

代码...

$.each(parent.window.frames, function(i,val){   
    if (parent.window.frames[i].Xrm.Page.data != null) {
           parent.window.frames[i].Xrm.Page.data.entity.attributes.get('ownerid').setValue([{ id: '{' + sourceKey + '}', name: name, entityType: "systemuser" }]);
           parent.window.frames[i].Xrm.Page.data.entity.save();
           break;
    }
});   

回答by Francis Ducharme

Based on TWilly's answer, I created the function below to retrieve the Xrmobject

根据 TWilly 的回答,我创建了下面的函数来检索Xrm对象

GetXrm: function () {

    var frame = $.grep(parent.window.frames, function (e) {
        if (e.Xrm.Page.data)
            return true;
     });

     return frame[0].Xrm;
}

回答by Lucia Clifford

Set Xrm before like this:

像这样设置 Xrm 之前:

  var Xrm = parent.Xrm;
    var Url = Xrm.Page.data.entity.attributes.getByName("attributename").getValue();