Archive for November, 2009

How to check for User Role in Microsoft Dynamic CRM 4.0 in Form Script

Thursday, November 19th, 2009

By using Microsoft Dynamic 4.0 form script capabilities, you can easily get the current user role. This ability will help you to create custom javascript to provide another level of user interaction. The script below will give you an example to grab all the roles assigned to the current CRM user.

First of all to make our life easier, as we do not need to reinvent the wheel (creating library to connect to CRM Web Service). Thanks to the guys at Ascentium, they have created a Microsoft Dynamics CRM JavaScript SDK which you can freely download at:

http://xrm.ascentium.com/blog/crm/Post129.aspx

For this example purpose, Copy and paste the Acentium CRM SDK javascript to the Form Onload of any entity.

Then below that, we can add our own code that refer to the script. Copy and paste the following.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// BEGIN:  ferolen  get the crrent user role scripts
var VALID_ROLENAME = "AdminGuy";
if (! UserHasRole(VALID_ROLENAME)) {
   // Do something if the user does not have the role
}
 
function UserHasRole(roleName)
{
   //create the Ascentium_CrmService object
   var oService = new Ascentium_CrmService();
   var xml = " ";
   var aoFetchResult = oService.Fetch(xml);
   for (var i = 0; i < aoFetchResult.length; i++) {
      var beResult = aoFetchResult[i];
      if (beResult.attributes["name"].value == roleName) return true;
   }
   return false;
}
// END : ferolen  get the crrent user role scripts