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

4

7

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:

<!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?

Thank you.

Fineness answered 14/2, 2014 at 21:52 Comment(0)
R
14

Try to access Xrm using following syntax:

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.

Rarefaction answered 14/2, 2014 at 22:17 Comment(2)
I have the same problem. I am running this in Chrome 37. I have a webresource being shown in an IFrame on an entity form. I have tried all combinations, but still cannot get my hands on the Xrm object. Help would be appreciated!Takin
You can do var Xrm = window.parent.Xrm; so each Xrm call does not need to be prefixed with window.parent. @Taha Ahmad, if Xrm is not defined, try frames[0].Xrm or frames[1].Xrm. So you can do var Xrm.Page.data = Xrm.Page.data || frames[0].Xrm.Page.data || frames[1].Xrm.Page.data || window.parent.Xrm.Page.data;. Similar to my answer about identifying dirty fields.Whiten
C
2

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

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;
    }
});   
Canvass answered 14/9, 2015 at 20:0 Comment(0)
P
1

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

GetXrm: function () {

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

     return frame[0].Xrm;
}
Perforated answered 31/1, 2017 at 18:45 Comment(0)
S
0

Set Xrm before like this:

  var Xrm = parent.Xrm;
    var Url = Xrm.Page.data.entity.attributes.getByName("attributename").getValue();
Selwyn answered 22/5, 2018 at 9:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.