D365 CE: Get Logged in User’s Security Roles using JavaScript

User Review
0 (0 votes)

Ajit Patra

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;

View original post 4 more words