[英]Cannot access Xrm.Page.data from within HTML web resource in CRM 2011
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. 我正在尝试从我已插入CRM 2011中的表单的HTML Web资源中访问Xrm.Page.data对象。但是,根据我尝试访问Xrm实体的方式,我发现它未定义或者Xrm.Page.data为null。 The code for the web resource is as follows:
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). 我已经从JavaScript函数中成功访问了Xrm.Page.data,该函数是触发表单事件的库的一部分(例如,Form.Load)。 It's just when it's embedded in an HTML web resource on the form that I run into this problem.
它只是当它嵌入到我遇到此问题的表单上的HTML Web资源中时。 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?
任何人都可以解释我做错了什么,如果有一种方法以我想做的方式访问Xrm.Page.data?
Thank you. 谢谢。
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. 从您的示例代码。
This works for when you have a web resource that is loaded within an iframe/dialog. 当您拥有在iframe /对话框中加载的Web资源时,此方法适用。 It gets access to the parent frame, then looks for all available frames, and checks which frame has
它可以访问父框架,然后查找所有可用的框架,并检查哪个框架具有
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;
}
});
Based on TWilly's answer, I created the function below to retrieve the Xrm
object 根据TWilly的回答,我创建了下面的函数来检索
Xrm
对象
GetXrm: function () {
var frame = $.grep(parent.window.frames, function (e) {
if (e.Xrm.Page.data)
return true;
});
return frame[0].Xrm;
}
Set Xrm before like this: 像这样设置Xrm:
var Xrm = parent.Xrm;
var Url = Xrm.Page.data.entity.attributes.getByName("attributename").getValue();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.