【问题标题】:Add team's administrator in TFS 2017 by TFS API REST or TFS SERVER API C#通过 TFS API REST 或 TFS SERVER API C# 在 TFS 2017 中添加团队管理员
【发布时间】:2017-08-07 21:16:47
【问题描述】:

我想通过使用 TFS API REST 或在 c# 中实现方法来添加团队管理员...您能帮帮我吗? 有没有办法通过代码来完成,而不是像您在图像中看到的那样从 tfs 网站完成? 谢谢!

【问题讨论】:

    标签: c# rest api


    【解决方案1】:

    目前,Rest API 无法实现这一点,即使将用户添加到团队也是如此。有一个相关的用户声音:

    添加rest api用于将成员添加到团队

    https://visualstudio.uservoice.com/forums/330519-team-services/suggestions/6088139-add-rest-api-for-adding-members-to-a-team

    您可以在服务器端使用 TFS API 服务器来实现,示例代码如下:

    static void Main(string[] args)
    {
        // Connect to the TFS server and get the team project URI.
        var collection = GetServer("server_uri");
        var projectUri = GetProjectUri(collection, "project_name");
    
        // Retrieve the default team.
        TfsTeamService teamService = collection.GetService<TfsTeamService>();
        TeamFoundationTeam defaultTeam = teamService.GetDefaultTeam(projectUri, null);
    
        // Get security namespace for the project collection.
        ISecurityService securityService = collection.GetService<ISecurityService>();
        SecurityNamespace securityNamespace = securityService.GetSecurityNamespace(FrameworkSecurity.IdentitiesNamespaceId);
    
        // Use reflection to retrieve a security token for the team.
        MethodInfo mi = typeof(IdentityHelper).GetMethod("CreateSecurityToken", BindingFlags.Static | BindingFlags.NonPublic);           
        string token = mi.Invoke(null, new object[] { defaultTeam.Identity }) as string;
    
        // Retrieve an ACL object for all the team members.
        var allMembers = defaultTeam.GetMembers(collection, MembershipQuery.Expanded).Where(m => !m.IsContainer);
        AccessControlList acl = securityNamespace.QueryAccessControlList(token, allMembers.Select(m => m.Descriptor), true);
    
        // Retrieve the team administrator SIDs by querying the ACL entries.
        var entries = acl.AccessControlEntries;
        var admins = entries.Where(e => (e.Allow & 15) == 15).Select(e => e.Descriptor.Identifier);
    
        // Finally, retrieve the actual TeamFoundationIdentity objects from the SIDs.
        var adminIdentities = allMembers.Where(m => admins.Contains(m.Descriptor.Identifier));       
    }
    

    虽然展示了如何查询团队管理员,但添加管理员也并不难。

    TeamFoundationIdentity admin
    
     string token = IdentityHelper.CreateSecurityToken(team.Identity); 
    
     securityNamespace.SetPermissions(token, admin.Descriptor, 15, 0, true);
    

    确保您使用的帐户具有足够的权限来添加团队管理员来运行代码。更多细节请参考问题中的这个答案:TFS API - How to get a Team's Adminstrator?

    【讨论】:

    • 谢谢!我只需要这样做来添加一个管理员:之后我得到了团队: string token = IdentityHelper.CreateSecurityToken(_team.Identity); securityNamespace.SetPermissions(token, admin.Descriptor, 15, 0, true);
    猜你喜欢
    • 1970-01-01
    • 2018-11-01
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 2016-03-06
    • 2016-09-03
    • 2023-03-24
    相关资源
    最近更新 更多