Vinz
Vinz

Reputation: 89

Get all members/users from TFS via REST API

I tried to get all members/users of TFS with the REST API and the .NET client libraries.

It works, but I get a maximum number of 50 identitys. Does anyone know, how I get all users, not only 50? (I prefer avoiding to use the old API, how it is suggested in this question)

Here is my code:

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

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

Upvotes: 4

Views: 5270

Answers (1)

Andy Li-MSFT
Andy Li-MSFT

Reputation: 30362

There is a REST API User Entitlements - List which can retrieve the user list from VSTS (Visual Studio Team Services), but it's only available for VSTS.

There isn't such a REST API to retrieve the user list from on-premise TFS (TFS 2017 in your scenario).

So, for now you can use the client API you mentioned above to retrieve the user list. Tested on my side, I can retrieve all the identities (more than 50 ) with below code:

You can also check the user list from userlist.txt file which under ..\..\ \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();
        }
    }
}

enter image description here

Upvotes: 5

Related Questions