通过 REST API 从 TFS 获取所有成员/用户

2023-12-24

我尝试使用 REST API 获取 TFS 的所有成员/用户.NET 客户端库 https://learn.microsoft.com/de-de/vsts/integrate/concepts/dotnet-client-libraries?view=vsts.

它有效,但我最多只能获得 50 个身份。有谁知道,我如何获得所有用户,而不仅仅是 50 个? (我更喜欢避免使用旧的 API,这里是如何建议的question https://stackoverflow.com/questions/44666525/is-there-an-sdk-api-or-rest-api-call-to-get-all-users-from-tfs-2017)

这是我的代码:

    VssCredentials credentials = new VssCredentials();
    VssConnection connection = new VssConnection(new Uri(url), credentials);

    IdentityMruHttpClient identityMruHttpClient = connection.GetClient<IdentityMruHttpClient>();
    List<IdentityRef> members = identityMruHttpClient.GetIdentityMruAsync(ProjectName).Result;

有一个 REST API用户权利 - 列表 https://learn.microsoft.com/zh-cn/rest/api/vsts/memberentitlementmanagement/user%20entitlements/list?view=vsts-rest-4.1它可以从 VSTS 检索用户列表(Visual Studio Team Services),但它仅适用于 VSTS。

没有这样的 REST API 可以从中检索用户列表本地部署TFS(您的场景中为 TFS 2017)。

所以,现在您可以使用客户端API https://www.nuget.org/packages/Microsoft.TeamFoundationServer.Client/您上面提到检索用户列表。在我这边进行测试,我可以使用以下代码检索所有身份(超过 50 个):

您还可以从以下位置查看用户列表userlist.txt文件位于..\..\ \bin\Debug\

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("http://server:8080/tfs"));

            IIdentityManagementService ims = tcs.GetService<IIdentityManagementService>();

            TeamFoundationIdentity tfi = ims.ReadIdentity(IdentitySearchFactor.AccountName, "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")

                    { Console.WriteLine(id.DisplayName); }
                    //{ Console.WriteLine(id.UniqueName); }

                    file.WriteLine("[{0}]", id.DisplayName);
                }

            var count = ids.Count(x => ids.Contains(x));
            Console.WriteLine(count);
            Console.ReadLine();
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过 REST API 从 TFS 获取所有成员/用户 的相关文章

随机推荐