JavaScript for checking current user Role

Sometimes in CRM2011 we need to show – hide some fields or do some actions on the form depending on the current user Role.

To archive this goal the easiest way, we can use the following Javascript functions described on the CRM2011 SDK.

The final code would look like this.

function MainCall()
{
                if(UserHasRole("Boss") == true )
                {
                //Show all the fields
                }
                else
                {
                //Hide some fields
                }
}

To be able to use this simple UserHasRole(“rolename”) function we need to add these 2 other Javascript subs.

</pre>
GetCurrentUserRoles = function() {

var xml = "" +

"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +

"<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\">" +

GenerateAuthenticationHeader() +

" <soap:Body>" +

" <RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +

" <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">" +

" <q1:EntityName>role</q1:EntityName>" +

" <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +

" <q1:Attributes>" +

" <q1:Attribute>name</q1:Attribute>" +

" </q1:Attributes>" +

" </q1:ColumnSet>" +

" <q1:Distinct>false</q1:Distinct>" +

" <q1:LinkEntities>" +

" <q1:LinkEntity>" +

" <q1:LinkFromAttributeName>roleid</q1:LinkFromAttributeName>" +

" <q1:LinkFromEntityName>role</q1:LinkFromEntityName>" +

" <q1:LinkToEntityName>systemuserroles</q1:LinkToEntityName>" +

" <q1:LinkToAttributeName>roleid</q1:LinkToAttributeName>" +

" <q1:JoinOperator>Inner</q1:JoinOperator>" +

" <q1:LinkEntities>" +

" <q1:LinkEntity>" +

" <q1:LinkFromAttributeName>systemuserid</q1:LinkFromAttributeName>" +

" <q1:LinkFromEntityName>systemuserroles</q1:LinkFromEntityName>" +

" <q1:LinkToEntityName>systemuser</q1:LinkToEntityName>" +

" <q1:LinkToAttributeName>systemuserid</q1:LinkToAttributeName>" +

" <q1:JoinOperator>Inner</q1:JoinOperator>" +

" <q1:LinkCriteria>" +

" <q1:FilterOperator>And</q1:FilterOperator>" +

" <q1:Conditions>" +

" <q1:Condition>" +

" <q1:AttributeName>systemuserid</q1:AttributeName>" +

" <q1:Operator>EqualUserId</q1:Operator>" +

" </q1:Condition>" +

" </q1:Conditions>" +

" </q1:LinkCriteria>" +

" </q1:LinkEntity>" +

" </q1:LinkEntities>" +

" </q1:LinkEntity>" +

" </q1:LinkEntities>" +

" </query>" +

" </RetrieveMultiple>" +

" </soap:Body>" +

"</soap:Envelope>" +

"";
<pre>

    var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");

    xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);

    xmlHttpRequest.setRequestHeader("SOAPAction", " http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");

    xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

    xmlHttpRequest.setRequestHeader("Content-Length", xml.length);

    xmlHttpRequest.send(xml);

    var resultXml = xmlHttpRequest.responseXML;

    return (resultXml);

}

function UserHasRole(roleName) {

                var oXml = GetCurrentUserRoles();

                if(oXml != null) {

                               // Role selection

                               var roles = oXml.selectNodes("//BusinessEntity/q1:name");

                               if(roles != null) {

                                               for( i = 0; i < roles.length; i++) {

                                                               if(roles[i].text == roleName)

                                                                              return true;

                                               }

                               }

                }

                return false;

}

Now that we know if a user has a role, we can use any of the Javascript simple commands to modify any value of the form based on Roles.

You can Download the full library crm_CommonLibrabryas a .doc document(cause word press dont allow .js attachments) . Download it and change the extension to .js to use it.

Thanks for reading.

Hache

Advertisements

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