【问题标题】:How to add member in VSTS team programmatically?如何以编程方式在 VSTS 团队中添加成员?
【发布时间】:2018-04-06 14:51:23
【问题描述】:

我想以编程方式在特定团队(在特定项目下)中添加用户/成员(已经拥有 VSTS 帐户)。从门户我们总是可以做同样的事情。但是,我正在寻找可以为我做同样事情的 REST 服务。

在以下来自微软的文档中,我们得到了几个与团队相关的 api。但它没有提供与“如何将现有 VSTS 帐户持有人添加到团队”相关的任何内容

https://www.visualstudio.com/en-us/docs/integrate/api/tfs/teams

提前致谢。

【问题讨论】:

    标签: tfs azure-devops azure-devops-rest-api


    【解决方案1】:

    选项 1:REST API(暂时不可用)

    目前,还没有这样的 REST API 可以将成员添加到团队项目中。 但是有用户声音add rest api for adding members to a team建议为REST API添加此功能,您可以投票和跟进。

    一旦 REST API 可用,您就可以通过编程方式使用 REST API 将成员添加到团队。

    选项 2:API

    您也可以参考cmets部分用户语音下的相关代码(默认团队添加成员示例):

    public Task<TeamProject> GetProjectAsync(string projectId) 
    { 
    var projectClient = this.Connection.GetClient<ProjectHttpClient>();
    
    return projectClient.GetProject(projectId); 
    } 
    public async Task GrantProjectAccessAsync(string projectId, string email) 
    { 
    var project = await this.GetProjectAsync(projectId);
    
    var client = this.Connection.GetClient<IdentityHttpClient>(); 
    var identities = await client.ReadIdentitiesAsync(Microsoft.VisualStudio.Services.Identity.IdentitySearchFilter.MailAddress, email); 
    if (!identities.Any() || identities.Count > 1) 
    { 
    throw new InvalidOperationException("User not found or could not get an exact match based on email"); 
    } 
    var userIdentity = identities.Single(); 
    var groupIdentity = await client.ReadIdentityAsync(project.DefaultTeam.Id); 
    
    await client.AddMemberToGroupAsync( 
    groupIdentity.Descriptor, 
    userIdentity.Id 
    ); 
    }
    

    要为另一个团队添加成员,您可以使用teamClient.GetTeamsAsync 方法获取该项目的团队,然后通过匹配给定的团队名称来获取团队ID。

    【讨论】:

    • 我更新了我的答案,您可以参考使用API​​选项以编程方式将成员添加到团队的方式。
    猜你喜欢
    • 1970-01-01
    • 2019-03-11
    • 1970-01-01
    • 1970-01-01
    • 2020-06-09
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    • 2021-07-17
    相关资源
    最近更新 更多