Rocco
Rocco

Reputation: 477

Trying to get list of TFS users trough client library

I'm trying to get a list of all teams on my TFS server(2017) with the following code and the client libraries version is the latest preview.

var teamclient = await Connection.GetClientAsync<TeamHttpClient>();
var teams = await teamclient.GetAllTeamsAsync();

This results in the following exception: API resource location 7a4d9ee9-3433-4347-b47a-7a80f1cf307e is not registered on (tfs link removed for privacy reasons)

Upvotes: 2

Views: 1649

Answers (1)

Cece Dong - MSFT
Cece Dong - MSFT

Reputation: 31003

You could try the following code to list all users:

TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri("url"));
tfs.EnsureAuthenticated();

IGroupSecurityService gss = tfs.GetService<IGroupSecurityService>();

Identity SIDS = gss.ReadIdentity(SearchFactor.AccountName, "Project Collection Valid Users", QueryMembership.Expanded);

Identity[] UserId = gss.ReadIdentities(SearchFactor.Sid, SIDS.Members, QueryMembership.None);

foreach (Identity user in UserId)
{
    Console.WriteLine(user.AccountName);
    Console.WriteLine(user.DisplayName);
}

Or you could use the following code to get users in each team:

using Microsoft.TeamFoundation.Core.WebApi;
using Microsoft.VisualStudio.Services.Client;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;
using System;
using System.Collections.Generic;

namespace GetUser
{
    class Program
    {
        static void Main(string[] args)
        {
            String collectionUri = "http://TFS2017:8080/tfs/defaultcollection";
            VssCredentials creds = new VssClientCredentials();
            creds.Storage = new VssClientCredentialStorage();
            VssConnection connection = new VssConnection(new Uri(collectionUri), creds);
            TeamHttpClient thc = connection.GetClient<TeamHttpClient>();
            List<IdentityRef> irs = thc.GetTeamMembersAsync("TeamProject", "TeamProjectTeam").Result;

            foreach (IdentityRef ir in irs)
            {
                Console.WriteLine(ir.DisplayName);
            }
        }
    }
}

Update:

using Microsoft.TeamFoundation.Core.WebApi;
using Microsoft.VisualStudio.Services.Client;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;
using System;
using System.Collections.Generic;

namespace GetUser
{
    class Program
    {
        static void Main(string[] args)
        {
                        String collectionUri = "http://TFS2017:8080/tfs/defaultcollection";
        VssCredentials creds = new VssClientCredentials();
        creds.Storage = new VssClientCredentialStorage();
        VssConnection connection = new VssConnection(new Uri(collectionUri), creds);
        TeamHttpClient thc = connection.GetClient<TeamHttpClient>();

        List<WebApiTeam> irs = thc.GetTeamsAsync("AgileProject").Result;

        foreach (WebApiTeam ir in irs)
        {
            Console.WriteLine(ir.Name);
        }

        }
    }
}

Upvotes: 3

Related Questions