【问题标题】:Devops server 2019 - Is there an REST api to add members to project and teamDevops server 2019 - 是否有 REST api 可以将成员添加到项目和团队
【发布时间】:2020-02-04 02:44:48
【问题描述】:

我希望使用 REST API 将成员添加到 Project。

我能够使用 API 创建项目:

POST https://{instance}/{collection}/_apis/projects?api-version=5.0

此外,我还能够使用 REST API 在项目中创建团队:

POST https://{instance}/{collection}/_apis/projects/{projectId}/teams?api-version=5.0

但是,我无法获得用于将成员添加到团队和项目的 REST API。

你能帮忙吗?

【问题讨论】:

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


    【解决方案1】:

    Devops server 2019 - 是否有 REST api 可以将成员添加到项目 和团队

    对于这个问题,我认为没有开箱即用的 rest api 来实现它。 Members - Add rest api 目前不适用于 Azure DevOps Server 2019。

    作为一种解决方法,我们可以通过在浏览器中按F12 然后选择Network 来跟踪这个rest api。

    示例请求网址:

    https://collectionName/DefaultCollection/projectId/_api/_identity/AddIdentities?__v=5
    

    示例请求正文:

    {
        "newUsersJson": "[]",
        "existingUsersJson": "[\"55b98726-c6f5-48d2-976b-xxxxxx\"]",
        "groupsToJoinJson": "[\"7283653f-54b2-4ebf-86c3-xxxxxxx\"]",
        "aadGroupsJson": "[]"
    }
    

    在这一步中,需要将要添加的成员名称和团队名称转换为json,然后添加到请求正文中。这是case 说明如何在 C# 中将字符串转换为 JSON。

    从这条记录中,我们可以得到请求的url和请求体。

    我用邮递员对此进行了测试,并且可以成功地将成员添加到项目团队中。

    这里有两个类似问题的案例(case1, case2)。您也可以参考它们。

    上述案例中的使用语音实例不再可用。您可以向我们的main forum 提交新的产品建议。我们的 PM 和产品组正在定期审查这些建议,并考虑将其作为计划。

    【讨论】:

    • 对延误表示歉意。经过几次尝试和错误,实施此解决方案。我现在可以添加已经存在于 tfs 上的成员以及域上的成员。感谢您的帮助。
    【解决方案2】:

    没有记录用于将成员添加到项目和团队的 REST API。正如 Hugh 所说,我们可以使用开发工具跟踪 REST API(在浏览器中按 F12),但是正如我们所见,我们只能在请求 json 正文中使用用户和团队/组 GUID。

    Post https://wsicads2019/DefaultCollection/{project}/_api/_identity/AddIdentities?api-version=5.0
    
    Request Body:
    
    {
        "newUsersJson": "[]",
        "existingUsersJson": "[\"55b98726-c6f5-48d2-976b-xxxxxx\"]",
        "groupsToJoinJson": "[\"7283653f-54b2-4ebf-86c3-xxxxxxx\"]",
        "aadGroupsJson": "[]"
    }
    

    对于特定的团队/组,我们可以使用REST APIs Projects 和团队来获取他们的 GUID。 对于用户来说,实际上是使用TeamFoundationId,唯一的TeamFoundationId是在用户添加到Azure DevOps Server时自动生成的。我们无法使用外部工具生成 ID。

    因此,要使用该 REST API,我们需要获取您想要将其添加到项目/团队的特定用户的 TeamFoundationId

    目前,在 Azure DevOps Server 2019 中没有列出 TeamFoundationId 用户的 REST API,但是我们可以通过客户端 API 获得它:

    以下示例供您参考以获取特定用户的TeamFoundationId:(它还将用户列表及其TeamFoundationId 导出到userlist.txt

    using System;
    using Microsoft.TeamFoundation.Client;
    using Microsoft.TeamFoundation.Framework.Client;
    using Microsoft.TeamFoundation.Framework.Common;
    using System.Linq;
    using System.IO;
    
    namespace Getuserlist
    
    {
    
        class Program
    
        {
            static void Main(string[] args)
    
            {
    
                TfsConfigurationServer tcs = new TfsConfigurationServer(new Uri("https://wsicads2019"));
    
                IIdentityManagementService ims = tcs.GetService<IIdentityManagementService>();
    
                TeamFoundationIdentity tfi = ims.ReadIdentity(IdentitySearchFactor.AccountName, "[DefaultCollection]\\Project Collection Valid Users", MembershipQuery.Expanded, ReadIdentityOptions.None);
    
                TeamFoundationIdentity[] ids = ims.ReadIdentities(tfi.Members, MembershipQuery.None, ReadIdentityOptions.None);
    
                using (StreamWriter file = new StreamWriter("userlist.txt"))
    
                    foreach (TeamFoundationIdentity id in ids)
    
                    {
                        if (id.Descriptor.IdentityType == "System.Security.Principal.WindowsIdentity" && id.UniqueName == "Domain\\User")
    
                        { Console.WriteLine("[{0},{1}]", id.UniqueName, id.TeamFoundationId); }
    
                        file.WriteLine("[{0},{1}]", id.UniqueName, id.TeamFoundationId);
                    }
    
                var count = ids.Count(x => ids.Contains(x));
                Console.WriteLine(count);
                Console.ReadLine();
            }
        }
    }
    

    【讨论】:

    • @Uday 您是否通过参考建议解决了问题?这里有更新吗?
    • 嘿,安迪,是的,我能够按照 Hugh 的建议实施更改。经过一些尝试和错误后,它起作用了。
    猜你喜欢
    • 2020-09-06
    • 2020-06-09
    • 1970-01-01
    • 2019-10-28
    • 2021-09-01
    • 2017-08-08
    • 2016-08-16
    • 1970-01-01
    • 2019-12-13
    相关资源
    最近更新 更多