How to get the Logged In User Security Role Names using JavaScript in Dynamics 365 V9.X?

Use the below code to get the Logged In User Security Role Names in Dynamics 365 V9.X,

JS Code:

function GetLoggedInUserSecurityRoleNames() {
 // Get Logged In User Context
 var userSettings = Xrm.Utility.getGlobalContext().userSettings;
 // Get Logged In User Security Roles
 var loggedInUsersecurityRolesGuidArray = userSettings.securityRoles;
 var totalSecurityRolesArray = new Array();
 var rolesOutputText = "";

if (loggedInUsersecurityRolesGuidArray.length > 0) {
 Xrm.WebApi.retrieveMultipleRecords("roles", "?$select=name,roleid").then(
 function success(result) {
 if (result.entities.length > 0) {
 // Push Role Names and Role Ids to Array
 for (var rolesCount = 0; rolesCount < result.entities.length; rolesCount++) {
 totalSecurityRolesArray.push({ RoleName: result.entities[rolesCount].name, RoleId: result.entities[rolesCount].roleid });
 }

rolesOutputText = userSettings.userName + " has the below Security Roles\n------------------------------------\n";

// Compare the User Security Roles with Total Security Roles
 for (var userSecurityRolesCounter = 0; userSecurityRolesCounter < loggedInUsersecurityRolesGuidArray.length; userSecurityRolesCounter++) {
 for (var totalsecurityRolesCounter = 0; totalsecurityRolesCounter < totalSecurityRolesArray.length; totalsecurityRolesCounter++) {
 if (totalSecurityRolesArray[totalsecurityRolesCounter].RoleId.toLowerCase() == loggedInUsersecurityRolesGuidArray[userSecurityRolesCounter].toLowerCase()) {
 rolesOutputText += totalSecurityRolesArray[totalsecurityRolesCounter].RoleName + "\n";
 break;
 }
 }
 }
 }

// Show User Roles
 Xrm.Utility.alertDialog(rolesOutputText, null);
 },
 function (error) {
 // Show error
 Xrm.Utility.alertDialog(error.message, null);
 });
 }
}

Output: Have configured the above function on Onload of Account and below is the output.

User Security Roles

Hope you got all the Security Role Names of the Logged In User :):):)

Advertisement

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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.