Dynamics CRM 2011插件如何获取用户所属的团队

时间:2013-05-01 22:35:59

标签: dynamics-crm-2011 crm microsoft-dynamics

我需要检查并查看用户是否在特定团队中。我假设我必须从团队开始,但我并不确定。只需查看并查看用户所属的所有团队就更容易了。有人在插件中有一个使用N:N关系的例子吗?

这是我到目前为止的代码......

// Set the properties of the QueryExpression object.
teamQuery.EntityName = "team";
teamQuery.AddAttributeValue("name", "Team");
teamQuery.ColumnSet = teamColumnSet;

EntityCollection teamDetail = service.RetrieveMultiple(teamQuery);

foreach (var teamDetail in teamDetail.Entities)
{
    teamGuid = teamDetail.Id;
}

感谢您的帮助!!

加里

2 个答案:

答案 0 :(得分:7)

这是我的最终解决方案......

QueryExpression teamQuery = new QueryExpression("team");
ColumnSet teamColumnSet = new ColumnSet("name");

teamQuery.ColumnSet = teamColumnSet;
teamQuery.Criteria = new FilterExpression();
teamQuery.Criteria.FilterOperator = LogicalOperator.And;
teamQuery.Criteria.AddCondition("name", ConditionOperator.Equal, "Sales");
teamQuery.AddLink("teammembership", "teamid", "teamid").AddLink ("systemuser", "systemuserid", "systemuserid").LinkCriteria.AddCondition("systemuserid", ConditionOperator.Equal, salesRepGuid);

 EntityCollection teamDetail = service.RetrieveMultiple(teamQuery);

加里

答案 1 :(得分:1)

您可以使用fetchXML。这将检入一个服务器请求而不是两个(一个获取teamid,第二个检查N:N关系)

            string fetchXML = @"
<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
  <entity name='systemuser'>
    <filter type='and'>
      <condition attribute='systemuserid' operator='eq' value='{0}' />
    </filter>
    <link-entity name='teammembership' from='systemuserid' to='systemuserid' visible='false' intersect='true'>
      <link-entity name='team' from='teamid' to='teamid' alias='ai'>
        <filter type='and'>
          <condition attribute='name' operator='eq' value='{1}' />
        </filter>
      </link-entity>
    </link-entity>
  </entity>
</fetch> ";

            Guid userID; //assign userid here. 
            string teamName = "team to check";
            fetchXML = string.Format(fetchXML, userID, teamName);

            EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetchXML));

            bool isUserInTeam = result.Entities.Count > 0;