Checking if a user has System admin role

Find below a C# method to check if an specified user(Sent as parameter) has System Admin role. The method will return true if so. Otherwise, it will return false.

/// <summary>
/// Check if user has System Admin role
/// </summary>
///  User GUID 
///  True if user is System Admin, false in other case 
public bool User_Is_SystemAdmin(Guid User_ID, IOrganizationService _service)
// All MS Dynamics CRM systems share the same System Admin role Guid.
// Hence, hardcoding it as this will not be a security issue
Guid Admin_GUID = new Guid("627090FF-40A3-4053-8790-584EDC5BE201");

// Defining Entity
var query = new QueryExpression("role");

// Adding validation to search based on Role GUID
query.Criteria.AddCondition("roletemplateid", ConditionOperator.Equal, Admin_GUID);

// Link to SystemUserRoles
var link = query.AddLink("systemuserroles", "roleid", "roleid");

// Adding validation to check for the specified user
link.LinkCriteria.AddCondition("systemuserid", ConditionOperator.Equal, User_ID);

// Return result
return _service.RetrieveMultiple(query).Entities.Count &gt; 0;

Hope it helps.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s