CRM2016 JavaScript – Context Object

Whilst working with Microsoft Dynamics CRM 2016 I have been playing around with the context object in JavaScript. In this quick post I will list some useful examples.

Including:

  • Getting details for the current user such as name and GUID.
  • Finding the role(s) of the current user.
  • Working out what client someone is running CRM from.
  • Returning the name of the current CRM organization.

Getting User Details

We can use the context object to get the current user name and GUID.

var userName = Xrm.Page.context.getUserName();
var userGUID = Xrm.Page.context.getUserId();
alert ("User is:" + userName + "nGUID is:" + userGUID);

Getting GUID of User Roles

The context option allows us to obtain an array contain the GUIDs for the roles of a user.

var userRoles = Xrm.Page.context.getUserRoles(); // *** Returns array of GUIDs for roles

for (var i = 0; i < userRoles.length; i++) {
  var RoleId = userRoles[i];
  alert("User Role GUID:" + RoleId);
}

I have actually created a post about this before! Here you can see how to build on this simple code to not only return the GUID but also the role name.

Get Details of the current client

We can use “.client” to find the current client, its state and form factor. (i.e. desktop, tablet etc.)

var formFactor = new Array();
formFactor[0] = "Unknown";
formFactor[1] = "Desktop";
formFactor[2] = "Tablet";
formFactor[3] = "Phone";

var client = Xrm.Page.context.client.getClient(); // *** Returns Browser, Outlook or Mobile
var clientState = Xrm.Page.context.client.getClientState(); // *** Returns Online or Offline
var clientFormFactor = Xrm.Page.context.client.getFormFactor(); // *** Returns 0-3

alert("Client:" + client + "nState:" + clientState + "nForm Factor:" + formFactor[clientFormFactor])

Get the Organization name

You can return you CRM organization name like this;

var OrgUniqueName = Xrm.Page.context.getOrgUniqueName();
alert("Organisation:" + OrgUniqueName);

You may find more useful features of the context object on the Microsoft site

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s