3

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

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
4

3 回答 3

4

这是我提取特定用户的所有角色的实现

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;
于 2013-03-05T14:49:30.950 回答
2

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

示例:确定用户是否具有角色

于 2013-03-04T21:07:57.670 回答
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 用法

于 2013-03-07T15:24:50.150 回答