问题
I can't seem to get my Xrm variable working when I create .js code. Is there a library I need to include or a function I need to run first? Do I need to make sure that ClientGlobalContext.js.aspx is referenced correctly? I'm in a 'C++' mindset and just wondering if there is any sort of 'include' command that I need to be running.
My js file looks like this, and if I remove the 'window.parent.' from the 2nd function the code breaks if I call it.
///<reference path="C:\Users\steve.lee\Downloads\XrmPage-vsdoc.js"/>
if (typeof (SDK) == "undefined")
{ SDK = { __namespace: true }; }
SDK.XRM = {
getCurrentControl: function () {
var currentControl = Xrm.Page.ui.getCurrentControl();
if (currentControl == null) {
alert("No controls currently have focus.");
}
else {
alert("The control for the '" + currentControl.getLabel() + "' attribute currently has focus.");
}
},
getCurrentGUID: function () {
if (window.parent.Xrm.Page.data.entity != null) {
var GUIDvalue = window.parent.Xrm.Page.data.entity.getId();
if (GUIDvalue != null) {
return GUIDvalue;
}
else {
return null;
}
}
else {
return null;
}
},
__namespace: true
};
回答1:
Is your code running as a javascript library added to a form or is it sat inside a web resource?
If its the latter, you need window.parent if its a HTML web resource. As this gets you access to the Xrm object on the form. In the parent window above it.
If it's a javascript library attached to the form properties then the XRM object is available by default.
The ClientGlobalContext.js.aspx is used when you have a web resource that may not be in the context of a form and still gives you access to the server url and the logged in user for example.
回答2:
The Xrm variable should be available without any includes or setup for normal JS code in CRM 2011.
The basics are covered here
来源:https://stackoverflow.com/questions/8434885/xrm-is-undefined-in-dynamics-crm-2011