如何获取systemuser的所有角色?

时间:2013-03-04 20:55:34

标签: dynamic dynamics-crm-2011

我正在尝试将所有角色分配给系统用户。我想我需要使用相关实体,但我不知道该如何继续使用该方法。 这是我的代码片段:

Uri organizationUri = new Uri(this.ConnectionString);
Uri homeRealmUri = null;
ClientCredentials credentials = new ClientCredentials();
credentials.UserName.UserName = ConfigUserName;
credentials.UserName.Password = ConfigPassword;

Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy orgProxy = new Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null);
IOrganizationService _service = (IOrganizationService)orgProxy;

Microsoft.Xrm.Sdk.Entity account = new Microsoft.Xrm.Sdk.Entity("systemuser");
QueryExpression query = new QueryExpression
{
    EntityName = account.LogicalName,
    ColumnSet = new ColumnSet(true)
};
DataCollection<Microsoft.Xrm.Sdk.Entity> users = _service.RetrieveMultiple(query).Entities;

// fetch assigned roles of users

3 个答案:

答案 0 :(得分:3)

这是我实现拉动特定用户的所有角色

QueryExpression query = new QueryExpression();
query.EntityName = "role";
query.ColumnSet = new ColumnSet(true);

LinkEntity role = new LinkEntity();
role.LinkFromEntityName = "role";
role.LinkFromAttributeName = "roleid";
role.LinkToEntityName = "systemuserroles";
role.LinkToAttributeName = "roleid";

LinkEntity userRoles = new LinkEntity();
userRoles.LinkFromEntityName = "systemuserroles";
userRoles.LinkFromAttributeName = "systemuserid";
userRoles.LinkToEntityName = "systemuser";
userRoles.LinkToAttributeName = "systemuserid";

ConditionExpression conditionExpression = new ConditionExpression();
conditionExpression.AttributeName = "systemuserid";
conditionExpression.Operator = ConditionOperator.Equal;
conditionExpression.Values.Add(userId);

userRoles.LinkCriteria = new FilterExpression();
userRoles.LinkCriteria.Conditions.Add(conditionExpression);

role.LinkEntities.Add(userRoles);
query.LinkEntities.Add(role);

DataCollection<Microsoft.Xrm.Sdk.Entity> userRoles = _service.RetrieveMultiple(query).Entities;
return userRoles;

答案 1 :(得分:2)

MSDN上有一个用于检查用户安全角色的示例,可以帮助您完成此操作。

Sample: Determine Whether a User has a Role

答案 2 :(得分:2)

使用生成的早期绑定CRM实体的以下Linq查询将完成您所追求的目标:

var query = from user in context.SystemUserSet
            join userRoles in context.SystemUserRolesSet on user.SystemUserId equals userRoles.SystemUserId
            join role in context.RoleSet on userRoles.RoleId equals role.RoleId
            where user.DomainName == '<username>'
            select role;

有关生成早期绑定实体的信息,请访问:CrmSvcUtil usage