Show hide form elements in Microsoft Dynamics CRM 4.0 based on user role

Because Microsoft Dynamics CRM 4.0 only allows a single form for each entity it is often necessary to show or hide certain form elements like buttons, iFrames and tabs based on the role of the currently logged in user.

Calling the CrmService of Microsoft Dynamics CRM 4.0 allow us to determine who the currently logged in user is and what roles they have been assigned.  I have attempted to keep this example straightforward the only code that has nothing to do with getting the current user and their role are the the two line turning off a tab in the event the user doesn’t have the role of “Manager”. = “hidden”; = “absolute”;

Everything else is specific to calling the CrmService getting the current user and finding their user roles in the XML DOM that the CrmService returns.

//Based on the role of the user logged in, hide the named tab on the entity form 


(currentUserHasRole(‘Manager’)) {else = = 


UserHasRole(roleName) {}function 



soapXml += GenerateAuthenticationHeader(); 

soapXml += soapBody; 

soapXml += 


xmlDoc = 

xmlDoc.async = 


getUserId() {try {var xmlhttp = new ActiveXObject(“Msxml2.XMLHTTP”);“POST”, “../../mscrmservices/2007/crmservice.asmx”, false);“Content-Type”, “text/xml; charset=utf-8″);“SOAPAction”, “”);var soapBody = “<soap:Body>” + “<Execute xmlns=’’>” + “<Request xsi:type=’WhoAmIRequest’ />” + “</Execute></soap:Body>”;var soapXml = “<soap:Envelope “ + “xmlns:soap=’’ “ + “xmlns:xsi=’’ “ + “xmlns:xsd=’’>”;“</soap:Envelope>”;new ActiveXObject(“Microsoft.XMLDOM”);false;var userid = xmlDoc.getElementsByTagName(“UserId”)[0].childNodes[0].nodeValue;return userid;catch (e) {return null;function 


getUserRoles(userId) {try {var command = new RemoteCommand(“UserManager”, “GetUserRoles”);“userIds”, “<guid>” + userId + “</guid>”);var oResult = command.Execute();if (oResult.Success) {return oResult.ReturnValue;catch (e) {return null;function 

result = getUserRoles(userId); 

oXml.resolveExternals = 

oXml.async = 


roleNode = oXml.selectSingleNode( 

hasRole = 

userHasRole(userId, roleName) {var hasRole = false;if (result != null) {var oXml = new ActiveXObject(“Microsoft.XMLDOM”);false;false;“/roles/role[name='” + roleName + “‘]“);if (roleNode != null) {if (roleNode.selectSingleNode(“roleid[@checked=’true’]“) != null) {true;return hasRole;function 

userId = getUserId(); 

currentUserHasRole(roleName) {return userHasRole(userId, roleName);


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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

%d bloggers like this: