Many times we come across requirements such as show/hide ribbon buttons based on logged in user’s security role.
Earlier, we used to get security roles of logged in user at client side using Xrm.Utility.getGlobalContext().userSettings.securityRoles which used to return array of GUID value of each security role.
Now that it’s deprecated, we can use Xrm.Utility.getGlobalContext().userSettings.roles which returns collection of objects with GUID and name of each security role that is assigned to the user directly or through the teams.
Below is the script which checks if the user has certain security roles based on names and hides the ribbon button if the user doesn’t have any of those security roles:
SAB.ShowHideReopenButton = function () {
var roles = Xrm.Utility.getGlobalContext().userSettings.roles;
if (roles === null) return false;
var hasRole = false;
roles.forEach(function (item) {
if (item.name.toLowerCase() === "cs manager" || item.name.toLowerCase() === "cs administrator") {
hasRole = true;
}
});
return hasRole;
}
Hope it helps !!
Reblogged this on Nishant Rana's Weblog.
LikeLike
how can i get the role name iam only able to get the guid
LikeLike
Using Xrm.Utility.getGlobalContext().userSettings.roles you should be able to get nae and guid of the security role as mentioned. Please see: https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/xrm-utility/getglobalcontext/usersettings#roles
LikeLike