How to detect a user is has administrator role in MS Dynamics CRM via SDK?
Asked Answered
I

1

5

I have a small issue with detecting whether the user in a company is an admin or not. A suggested way to do that by MS is to query for a role name "administrator" etc.

BUT the thing is that it is for some reason the role names seem to be translated, so it makes it a bit difficult to query for it in different languages, i.e. what was "administrator" could now be an "администратор".

*Using the role id does not seem to work either, on different version of CRM at least.

Have anybody ever struggled with such a thing? Gladly appreciate your help!

Indefinite answered 19/1, 2016 at 16:20 Comment(0)
D
9

The system administrator role can be identified using the ID of a role template. For built-in security roles Dynamics CRM systems all share the same Guids, so you can simply hard code your language-agnostic query.

Here a code sample. (In this example _service should be an object implementing the IOrganizationService interface.)

private static readonly Guid AdminRoleTemplateId = new Guid("627090FF-40A3-4053-8790-584EDC5BE201");

public bool HavingAdminRole(Guid systemUserId)
{
    var query = new QueryExpression("role");
    query.Criteria.AddCondition("roletemplateid", ConditionOperator.Equal, AdminRoleTemplateId);
    var link = query.AddLink("systemuserroles", "roleid", "roleid");
    link.LinkCriteria.AddCondition("systemuserid", ConditionOperator.Equal, systemUserId);

    return _service.RetrieveMultiple(query).Entities.Count > 0;
}
Definiendum answered 19/1, 2016 at 17:27 Comment(4)
Thank you, Henk, that did help me!Indefinite
Where are the built-in security roles and their guids documented?Association
Actually I found some GUIDs by accessing https://<myorg>.dynamics.com/api/data/v9.0/roles and finding all the roles with a non-null _roletemplateid_value. Presumably all the built-in roles can be accessed similarly, though if anyone can point me to more concrete documentation I'd be happy.Association
@andrea: a.f.i.k. these Guids are not documented. Just query them like you did.Definiendum

© 2022 - 2024 — McMap. All rights reserved.