Search code examples
javascriptdynamics-crm

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


I'm trying access the Xrm.Page.data object from within an HTML web resource. I try to access the Xrm context, I find that it is undefined or that Xrm.Page is null. The code for the web resource is as follows:

function GetClientUrl() {
        if (typeof window.parent.Xrm.Page.context == "object") {
            clientUrl = window.parent.Xrm.Page.context.getClientUrl();
        }
        var ServicePath = "/XRMServices/2011/Organization.svc/web";
        return clientUrl + ServicePath;
    }

It's just appen when it's embedded in an HTML web resource. Can anyone explain what I'm doing wrong? Thank you.


Solution

  • Microsoft provided a function to get the context for web resources.

    You need to add a reference to:

    <script src="../ClientGlobalContext.js.aspx" type="text/javascript" ></script>
    

    Then you can get the context like so:

    var context = GetGlobalContext();
    clientUrl = context.getClientUrl();
    

    Documentation: GetGlobalContext function and ClientGlobalContext.js.aspx (client-side reference)