In this post, I am going to demonstrate how to check that current logged in user has specific role or not in Dynamics CRM 365 using JavaScript & Web API.
Dependency:
Need this library to be included in form load / page load
AWXRMLibrary
㉺㉼㉴㉳㉽㉾㈕㈔㈆㈅㈄㈄㈃㈁㈀㉤
Ali Hamza Wadood
Microsoft Dynamics CRM Developer | Software Engineer - Microsoft Technologies(Asp.Net, Asp.Net MVC)
LinkedIn
function IsRoleExist(RoleName) {
var currentUserRoles = Xrm.Page.context.getUserRoles();
for (var i = 0; i < currentUserRoles.length; i++) {
var userRoleId = currentUserRoles[i];
if (GetRoleName(userRoleId) == RoleName) {
return true;
}
}
return false;
}
function GetRoleName(RoleId) {
var select = "roles(" + RoleId + ")?$select=name";
var result = AWXRM.WebAPISelectVersion(select, "8.2");
var Rolename = result["name"];
return Rolename;
}
function OnLoad() {
var Role_Admin = "System Admin_1";
var IsRoleExist = null;
if (IsRoleExist(Role_Admin))
IsRoleExist = true;
else
IsRoleExist = false;
}
var currentUserRoles = Xrm.Page.context.getUserRoles();
for (var i = 0; i < currentUserRoles.length; i++) {
var userRoleId = currentUserRoles[i];
if (GetRoleName(userRoleId) == RoleName) {
return true;
}
}
return false;
}
function GetRoleName(RoleId) {
var select = "roles(" + RoleId + ")?$select=name";
var result = AWXRM.WebAPISelectVersion(select, "8.2");
var Rolename = result["name"];
return Rolename;
}
function OnLoad() {
var Role_Admin = "System Admin_1";
var IsRoleExist = null;
if (IsRoleExist(Role_Admin))
IsRoleExist = true;
else
IsRoleExist = false;
}
Dependency:
Need this library to be included in form load / page load
AWXRMLibrary
㉺㉼㉴㉳㉽㉾㈕㈔㈆㈅㈄㈄㈃㈁㈀㉤
Ali Hamza Wadood
Microsoft Dynamics CRM Developer | Software Engineer - Microsoft Technologies(Asp.Net, Asp.Net MVC)