Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 357
anton Avatar asked Jan 19 '16 16:01

anton


People also ask

How do you check which Users has which Security roles in dynamics?

Navigate to Settings > System > Security. Select the Security roles icon. You now see a list of security roles.

How do I see roles in Dynamics 365?

To assign a role in Dynamics 365 Sales Professional: Under Standard Settings, select Manage users. Select a user you want to assign a role to, and then on the command bar, select Manage Roles. In the Manage User Roles dialog box, select the security role or roles you want for the user or users, and then select OK.


1 Answers

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;
}
like image 131
Henk van Boeijen Avatar answered Oct 30 '22 02:10

Henk van Boeijen