Sample: SDK.ContextSamples.js

[Applies to: Microsoft Dynamics CRM 2011]

This sample code is for Microsoft Dynamics CRM 2011, and can be found in the following location in the SDK download: SDK\SampleCode\JS\FormScripts\SDK.ContextSamples.js

Requirements

This sample code represents a JScript library that can be added as a Web resource. Before a function in a JScript library can be used as a form event handler, the JScript library must be added to the form libraries available for that form. The full name of the function must be used in the form event handler. For example, the getOrgAndUserInfo function must be called using SDK.ContextSamples.getOrgAndUserInfo.

Demonstrates

This library contains examples of the following Xrm.Page.context methods. These methods are also available in a context object returned by the GetGlobalContext Function.

Example

//If the SDK namespace object is not defined, create it.
if (typeof (SDK) == "undefined")
{ SDK = {}; }
// Create Namespace container for functions in this library;
SDK.ContextSamples = {};


 // Xrm.Page.context.getAuthenticationHeader() example
// Example: The SDK.ContextSamples.getAuthenticationHeader function alerts the user with the value of the authentication header.
SDK.ContextSamples.getAuthenticationHeader = function () {
 var authenticationHeader = Xrm.Page.context.getAuthenticationHeader();

 alert("The Authentication Header is:\n\n" + authenticationHeader);
};

 // Xrm.Page.context.getOrgLcid() example
 // Xrm.Page.context.getOrgUniqueName example
 // Xrm.Page.context.getUserLcid() example
// Example: The SDK.ContextSamples.getOrgAndUserInfo function alerts the organization's name and language code, as well as the current user's language code.
SDK.ContextSamples.getOrgAndUserInfo = function () {
 var orgName = Xrm.Page.context.getOrgUniqueName();
 var message = "The current Organization Name is '" + orgName + "'.\n";

 var orgLcid = Xrm.Page.context.getOrgLcid();
 message += "The current Organization Language Code Id is '" + orgLcid + "'.\n";

 var currentUserLcid = Xrm.Page.context.getUserLcid();
 message += "The current user's Language Code Id is '" + currentUserLcid + "'.";

 alert(message);
};

 // Xrm.Page.context.getServerUrl example
 // Xrm.Page.context.getUserRoles example
// Example: The SDK.ContextSamples.isUserSysAdmin function calls the CRM oData Endpoint to get the id of the System Administrator role, 
 // and then checks if the current user has the System Administrator role.
 // This function has a dependency on a JSON object that might need to be created using the json2.js file.
SDK.ContextSamples.isUserSysAdmin = function () {
 var serverUrl = Xrm.Page.context.getServerUrl();

 if (serverUrl.match(/\/$/)) {
  serverUrl = serverUrl.substring(0, serverUrl.length - 1);
 }

 var query = "/XRMServices/2011/OrganizationData.svc/RoleSet?$top=1&$filter=Name eq 'System Administrator'&$select=RoleId";
 var retrieveRoleRequest = new XMLHttpRequest();
 retrieveRoleRequest.open("GET", serverUrl + query, true);
 retrieveRoleRequest.setRequestHeader("Accept", "application/json");
 retrieveRoleRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
 retrieveRoleRequest.onreadystatechange = function () {
  SDK.ContextSamples.retrieveRoleResponse(this);
 };
 retrieveRoleRequest.send();
};
SDK.ContextSamples.retrieveRoleResponse = function (retrieveRoleRequest) {
 if (retrieveRoleRequest.readyState == 4) {
  if (retrieveRoleRequest.status == 200) {

   var results = JSON.parse(retrieveRoleRequest.responseText).d.results;

   if (results[0] != null) {
    var sysAdminRoleId = results[0].RoleId;

    var currentUserRoles = Xrm.Page.context.getUserRoles();

    for (var i = 0; i < currentUserRoles.length; i++) {
     var userRole = currentUserRoles[i];

     if (userRole == sysAdminRoleId) {
      alert("The current user has the 'System Administrator' role.");
      return;
     }
    }
    alert("The current user does not have the 'System Administrator' role.");

   }
   else {
    alert("The 'System Administrator' role id could not be found");
   }
  }
  else {
   //handle error
  }
 }
};

 // Xrm.Page.context.getUserId() example
// Example: The SDK.ContextSamples.isCurrentUserRecordOwner function checks if the current user's id is equal to the current record's ownerid.
SDK.ContextSamples.isCurrenUserRecordOwner = function () {
 var currentUserId = Xrm.Page.context.getUserId();

 var ownerIdAttribute = Xrm.Page.data.entity.attributes.get("ownerid");
 if (ownerIdAttribute != null) {

  var ownerIdValue = ownerIdAttribute.getValue();
  if (ownerIdValue != null && ownerIdValue.length == 1) {
   if (currentUserId == ownerIdValue[0].id) {
    alert("The current user is the owner of this record.");
   }
   else {
    alert("The current user is not the owner of this record.");
   }
  }
  else {
   alert("The ownerid field does not currently have a value.");
  }
 }
 else {
  alert("The ownerid field is not on the current form.");
 }
};

 // Xrm.Page.context.getQueryStringParameters() example
// Example: The SDK.ContextSamples.getQueryStringParams function displays all of the query string parameters and their values.
SDK.ContextSamples.getQueryStringParams = function () {
 var queryStringParams = Xrm.Page.context.getQueryStringParameters();

 var paramsMessage = "";
 for (var i in queryStringParams) {
  paramsMessage += "\n" + i + ": " + queryStringParams[i];
 }

 alert("The current query string parameters are:\n" + paramsMessage);
};
 //End of Context Samples Functions

See Also

Reference

Xrm.Page.context
Client-Side Context Reference
GetGlobalContext Function

Concepts

Write Code for Microsoft Dynamics CRM Forms
Use JavaScript with Microsoft Dynamics CRM

Other Resources

Xrm.Page Sample Libraries

Microsoft Dynamics CRM 2011
Send comments about this topic to Microsoft.
© 2013 Microsoft Corporation. All rights reserved.