Tuesday, 17 September 2013

Retrieve System Users by a Security Role (CRM 2011)

We have had the need to retrieve an entity collection of system users from a security role in a plugin. We developed a method that accept the role's name and a collection of attributes of System User entity.
In this method we use query expression to retrieve the system user specifying a link to entity "SystemUserRoles" and then a link to the "Role" entity. The linking attribute is the SystemUserId for the first link and the RoleId for the second one.
Finally a condition on the role's name to filter the system users.

public EntityCollection RetrieveUsersInfoByRole(string roleName,
                                                string[] userColumnSet)
{
    try
    {
        serviceProxy = ServiceConnector.
                       GetOrganizationService("<Organization.svc URL>");
 
        QueryExpression query = new QueryExpression("systemuser");
        query.ColumnSet = new ColumnSet(userColumnSet);
        query.Distinct = true;
        query.Criteria = new FilterExpression();
        //Add link to system user roles and role
        query.AddLink("systemuserroles", "systemuserid", "systemuserid").
            AddLink("role", "roleid", "roleid").LinkCriteria.
            AddCondition("name", ConditionOperator.Equal, roleName);
 
        EntityCollection users = serviceProxy.RetrieveMultiple(query);
 
        return users;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

 
Hope it can be useful.
Happy CRM coding

Rate this posting:
{[['']]}

1 comment:

  1. My friend recommended this blog and he was totally right keep up the fantastic work!
    MathCad Online Project Help

    ReplyDelete