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


Applies To: Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2016, Dynamics 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.

GetGlobalContext function

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(); }
  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.


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 JavaScript web resource named ‘sdk_/Scripts/ContosoScript.js’ with a CSS web resource named ‘sdk_/Styles/ContosoStyles.css’.

    <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" />


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.

See Also

Client-side programming reference
Client-side context (client-side reference)
Execution context (client-side reference)
Form scripting quick reference
Write code for Microsoft Dynamics 365 forms
Use the Xrm.Page object model

Microsoft Dynamics 365

© 2017 Microsoft. All rights reserved. Copyright