c# – 如何以编程方式在MS CRM业务部门中获取具有特定角色的用户
作者:互联网
我正在尝试通过C#插件从MS CRM 2011业务部门检索具有特定角色的用户,但是我坚持为此编写正确的查询.通过N:N关系链接到用户的角色,我很难找到这种情况的示例查询.
现在我想出了以下内容:
var entity = organizationService.Retrieve(entityName, entityId, new ColumnSet(new string[] { "new_unit" }));
if (entity.Attributes.Keys.Contains("new_unit"))
{
QueryExpression query = new QueryExpression("systemuser");
query.ColumnSet = new ColumnSet(new string[] { "systemuserid" });
query.Distinct = true;
query.Criteria = new FilterExpression();
query.Criteria.AddCondition("businessunitid", ConditionOperator.Equal, ((EntityReference)entity.Attributes["new_unit"]).Id);
}
我不确定我需要哪个实体链接systemuser以及如何实现具有特定角色和业务单位的目标检索用户.
我可以很容易地得到角色的名称或它的Guid,但我接下来要做什么呢?
解决方法:
您必须使用AddLink方法来执行连接.
这应该是你需要的:
QueryExpression query = new QueryExpression("systemuser");
query.ColumnSet = new ColumnSet(new string[] { "systemuserid" });
query.Distinct = true;
query.Criteria = new FilterExpression();
query.Criteria.AddCondition("businessunitid", ConditionOperator.Equal, ((EntityReference)entity.Attributes["new_unit"]).Id);
query.AddLink("systemuserroles", "systemuserid", "systemuserid").
AddLink("role","roleid", "roleid").
LinkCriteria.AddCondition("name", ConditionOperator.Equal, "MyRoleName");
var users = organizationService.RetrieveMultiple(query);
如果您可以轻松获取RoleId,则可以跳过添加指向Role Entity的链接,只需将LinkCriteria条件添加到SystemUserRoles实体:
QueryExpression query = new QueryExpression("systemuser");
query.ColumnSet = new ColumnSet(new string[] { "systemuserid" });
query.Distinct = true;
query.Criteria = new FilterExpression();
query.Criteria.AddCondition("businessunitid", ConditionOperator.Equal, ((EntityReference)entity.Attributes["new_unit"]).Id);
query.AddLink("systemuserroles", "systemuserid", "systemuserid").
LinkCriteria.AddCondition("roleid", ConditionOperator.Equal, roleIdGuid);
var users = organizationService.RetrieveMultiple(query);
标签:c,dynamics-crm,dynamics-crm-2011 来源: https://codeday.me/bug/20190625/1285579.html