如何在 Dynamics 365 C# 插件中查询用户的团队?

How do I query a user's teams in Dynamics 365 C# plugin?

此处显示的示例传入 userID 和一个安全名称字符串数组。如果用户具有与名称匹配的任何安全角色,则 return 为真。

public static bool UserHasRoles(IOrganizationService service, Guid userId, params string[] roleNames)
{
        var query = new QueryExpression("systemuserroles");
        query.TopCount = 1; 
        query.ColumnSet.AddColumns("systemuserroleid");           
        query.Criteria.AddCondition("systemuserid", ConditionOperator.Equal, userId);
        
        var query_role = query.AddLink("role", "roleid", "roleid");            
        query_role.LinkCriteria.AddCondition("name", ConditionOperator.In, roleNames);
        
        var result = service.RetrieveMultiple(query);
        return result.Entities.Any();
}

我需要重新创建此代码,但如果用户属于任何包含相同名称的团队,则 return 为真。以下是我的尝试:

public static bool UserHasRoles(IOrganizationService service, Guid userId, params string[] roleNames)
{
        var query = new QueryExpression("teammembership");
        query.TopCount = 1; 
        query.ColumnSet.AddColumns("teamMembershipId");           
        query.Criteria.AddCondition("systemuserid", ConditionOperator.Equal, userId);

        var query_role = query.AddLink("team", "teamid", "teamid");
        query_role.LinkCriteria.AddCondition("name", ConditionOperator.In, roleNames);
        
        var result = service.RetrieveMultiple(query);

        return result.Entities.Any();
}

当运行代码时,我得到一个错误

'TeamMembership' entity doesn't contain attribute with Name = 'teamMembershipId' and NameMapping = 'Logical'

我不确定我是否正确查询了等效实体。或者我可能会以完全错误的方式解决这个问题。帮助会很棒。

实际上,您不需要返回任何列。
这是没有 ColumnSet 行的代码,我还更正了您的方法名称和参数名称:

public static bool UserInTeams(IOrganizationService service, Guid userId, params string[] teamNames)
{
    var query = new QueryExpression("teammembership");
    query.TopCount = 1;
    query.Criteria.AddCondition("systemuserid", ConditionOperator.Equal, userId);
    
    var query_role = query.AddLink("team", "teamid", "teamid");
    query_role.LinkCriteria.AddCondition("name", ConditionOperator.In, teamNames);
    
    var result = service.RetrieveMultiple(query);
    return result.Entities.Any();
}