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”.

    crmForm.all.tab2Tab.style.visibility = “hidden”;

    crmForm.all.tab2Tab.style.position = “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 

if 

(currentUserHasRole(‘Manager’)) {else 

crmForm.all.tab2Tab.style.visibility = 

crmForm.all.tab2Tab.style.position = 

{“hidden”;“absolute”;function 

UserHasRole(roleName) {}function 

xmlhttp.open( 

xmlhttp.setRequestHeader( 

xmlhttp.setRequestHeader( 

soapXml += GenerateAuthenticationHeader(); 

soapXml += soapBody; 

soapXml += 

xmlhttp.send(soapXml); 

xmlDoc = 

xmlDoc.async = 

xmlDoc.loadXML(xmlhttp.responseXML.xml); 

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

command.SetParameter( 

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 = 

oXml.loadXML(result); 

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:

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 )

Google+ photo

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

Connecting to %s


%d bloggers like this: