Export (0) Print
Expand All

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

Applies To: CRM 2015 on-prem, CRM Online

Use the GetGlobalContext function when programming with web resources to gain access to context information. To get the GetGlobalContext function in your HTML web resource, include a reference to ClientGlobalContext.js.aspx.

The GetGlobalContext function returns the same context object found in the Xrm.Page.context.

When you need context information outside a form, include a reference to the ClientGlobalContext.js.aspx page in an HTML web resource.

The following example shows a function designed to return a context object when used in either a form script or a script used with an HTML web resource.

  function _getContext() {
   var errorMessage = "Context is not available.";
   if (typeof GetGlobalContext != "undefined")
   { return GetGlobalContext(); }
   else
   {
    if (typeof Xrm != "undefined") {
     return Xrm.Page.context;
    }
    else { throw new Error(errorMessage); }
   }
  }

You can use the GetGlobalContext function when you include a reference to the ClientGlobalContext.js.aspx page located at the root of the web resources directory.

noteNote
If you are not using backslash characters in HTML web resource names to simulate a folder structure, you can include this script in your page using this script element: <script type="text/javascript" src="ClientGlobalContext.js.aspx"></script>

If you are using backslash characters in HTML web resource names to simulate a directory structure, you must reflect this in your script element. The following example is for an HTML web resource named ‘sdk_/Contoso.htm’ and a JScript web resource named ‘sdk_/Scripts/ContosoScript.js’ with a CSS web resource named ‘sdk_/Styles/ContosoStyles.css’.

<head>
    <title>HTML Web Resource</title>
    <script src="../ClientGlobalContext.js.aspx" type="text/javascript" ></script>

    <script src="Scripts/ContosoScript.js" type="text/javascript"></script>
    <link href="Styles/ContosoStyles.css" rel="stylesheet" type="text/css" />
</head>

noteNote
Using a relative path including the root WebResources folder, for example, /WebResources/ClientGlobalContext.js.aspx, is not recommended because it can cause the page to lose organization context in a multi-tenant environment.

The ClientGlobalContext.js.aspx page will include some global event handlers. These event handlers will cancel the onselectstart, contextmenu, and ondragstart events.

Microsoft Dynamics CRM 2015 and Microsoft Dynamics CRM Online
Send comments about this topic to Microsoft.
© 2014 Microsoft Corporation. All rights reserved.
Show:
© 2014 Microsoft