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

Vin*_*inz 4 c# tfs

我尝试使用 REST API 和.NET 客户端库获取 TFS 的所有成员/用户。

它有效,但我最多获得 50 个身份。有谁知道,我如何获得所有用户,而不仅仅是 50 个?(我更喜欢避免使用旧的 API,在这个问题中是如何建议的)

这是我的代码:

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

    IdentityMruHttpClient identityMruHttpClient = connection.GetClient<IdentityMruHttpClient>();
    List<IdentityRef> members = identityMruHttpClient.GetIdentityMruAsync(ProjectName).Result;
Run Code Online (Sandbox Code Playgroud)

And*_*SFT 5

有一个 REST API User Entitlements - List可以从 VSTS ( Visual Studio Team Services)检索用户列表,但它仅适用于 VSTS。

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

因此,现在您可以使用上面提到的客户端 API来检索用户列表。在我这边进行了测试,我可以使用以下代码检索所有身份(超过 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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明