Hsl.currentUser
RoleKeysIds
Roles in Dynamics 365 will have different roleids in each organization and each business unit so using roleids directly is not a good cross-environment approach to determining whether the current user has a particular role. Using role names can lead to unexpected issues if someone renames a particular role. Luckily, rules still have fields that stay the same when deploying between organizations. For Out of the Box roles, this is roletemplateid. For custom roles, it is parentrootroleid.
Therefore, hasRole, hasRoles, and hasAnyOfRoles use roleKeyIds. The mapping between roleids and rolekeys is cached so the data is only queried once and then stored in the browser. This makes these methods extremely quick and allow them to avoid an API call in most cases.
To check the ids for a particular role by name, use visit this url replacing 'System Adminstrator' with the role you're searching for. Use roletemplateid
if the role has one, otherwise, use parentrootroleid
.
<orgbaseurl>/api/data/v9.1/roles?$select=name,_parentrootroleid_value,_roletemplateid_value&$filter=_parentroleid_value%20eq%20null%20and%20name%20eq%20'System Administrator'
Which returns a response like
{
"@odata.context": "<orgurl>/api/data/v9.1/$metadata#roles(name,_parentrootroleid_value,_roletemplateid_value)",
"value": [
{
"@odata.etag": "W/\"6582097\"",
"name": "System Administrator",
"_parentrootroleid_value": "7cb2af00-9da4-e711-a95b-000d3a367d35",
"_roletemplateid_value": "627090ff-40a3-4053-8790-584edc5be201",
"roleid": "7cb2af00-9da4-e711-a95b-000d3a367d35"
}
]
}
We find that the key for system administrator is 627090ff-40a3-4053-8790-584edc5be201
.
So we'd use Hsl.currentUser.hasRole('627090ff-40a3-4053-8790-584edc5be201')
to determine whether the current use is a system admin.
You can also use the following snippet to report the ids to use for each role in your organization.
Hsl.WebApi.getClient('9.1').query('role', {
select: 'name,_parentrootroleid_value,_roletemplateid_value',
filter: '_parentroleid_value eq null',
orderby: 'name asc',
}).then(function (c) {
c.value.forEach(function (role) {
var key = role.getValue('_roletemplateid_value') || role.getValue('_parentrootroleid_value');
console.log(role.getValue('name') + ': ' + key);
});
});
hasRole
Hsl.currentUser.hasRole(roleKeyId: string, opts?: UserInformationRequestSettings): Promise<boolean>
Returns whether the current user has the role for the specified RoleKey.
hasRoles
Hsl.currentUser.hasRoles(roleKeyIds: string[], opts?: UserInformationRequestSettings): Promise<boolean>
Returns whether the current user has the role for all of the specified RoleKeys.
hasAnyOfRoles
Hsl.currentUser.hasAnyOfRoles(roleKeyIds: string[], opts?: UserInformationRequestSettings): Promise<boolean>
Returns whether the current user has the role for any of the specified RoleKeys.
hasPrivilege
Hsl.currentUser.hasPrivilege(privName: string, opts?: UserInformationRequestSettings): Promise<boolean>
Returns whether the current user has the specified privilege.
hasPrivileges
Hsl.currentUser.hasPrivileges(privNames: string[], opts?: UserInformationRequestSettings): Promise<boolean>
Returns whether the current user has all of the specified privileges.