Wednesday, 5 August 2020

Get the Logged In User Security Role Names using JavaScript in Dynamics 365

Description:

In this example we explain that how to retrieve the current user logged in to Dynamic 365 CRM.or how to retrieve or get the Logged In user security Role Name in Dynamic 365 using JavaScript. Or how to get User security role name from context in Dynamic 365 using JavaScript. Or get security role name of the Logged In user using JavaScript.


So below is the JavaScript code that will help you to get the current Logged In user security role name in Dynamic 365.or how to get All Security Role Names of the Logged In User in Dynamic 365 using JavaScript.

 

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 allRolesName = "";

 

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 });

 }

 

allRolesName = 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()) {

 allRolesName += totalSecurityRolesArray[totalsecurityRolesCounter].RoleName + "\n";

 break;

 }

 }

 }

 }

 

// Show User Roles

 Xrm.Utility.alertDialog(allRolesName, null);

 },

 function (error) {

 // Show error

 Xrm.Utility.alertDialog(error.message, null);

 });

 }

}


0 comments:

Post a Comment