首页 > 解决方案 > 将成员添加到 Azure DevOps 团队

问题描述

我已经能够获得团队或团队成员,但我无法将用户添加到团队中。是否有我可以使用域名“domain\user”将用户添加到团队的 REST_API、命令行或 API。请指教。非常感谢

此致,

标签: powershellcommand-linetfsazure-devops

解决方案


如果您使用 Azure DevOps 服务 ( https://dev.azure.com/xxxx ),您可以使用成员 - 添加REST API。

如果使用 Azure DevOps Server,则未记录用于将成员添加到项目和团队的 REST API。作为一种解决方法,我们可以通过F12在浏览器中按下然后选择来跟踪这个 rest api Network

样本:

POST http://TFS2019:8080/tfs/{Collection}/{project}/_api/_identity/AddIdentities?api-version=5.0

Request Body:

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

但是,正如我们所见,我们只能在请求 json 正文中使用用户和团队/组 GUID。对于特定的团队/组,我们可以使用REST API 项目和团队来获取他们的 GUID。对于用户而言,实际上是使用,将用户添加到 Azure DevOps Server 时会自动生成TeamFoundationId唯一性。TeamFoundationId我们无法使用外部工具生成 ID。

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

目前,没有 REST API 来列出TeamFoundationIdAzure DevOps Server 2019 中的用户,但是我们可以使用 Client API 获取它:

下面的示例供您参考以获取TeamFoundationId特定用户的:(它还将导出用户列表及其TeamFoundationIduserlist.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();
        }
    }
}

推荐阅读