Luis Gouveia
Luis Gouveia

Reputation: 8925

Programatically access TFS account to get project names and work items

I've been trying to access a TFS account (programatically using the TFS SDK) in order to get information such as the project names, work item names and developpment times for each work item.

I wasn't able to get project or work item information, but I was able to authenticate and access TFS using the class TfsTeamProjectCollection.

However, using the class TfsConfigurationServer, I was never able to authenticate to the server. This is a pity because the majority of examples I've seen in the web use this TfsConfigurationServer class.

The code that allows me to access TFS:

// Connect to Team Foundation Server.
NetworkCredential netCred = new NetworkCredential("[email protected]", "myPassword");
BasicAuthCredential basicCred = new BasicAuthCredential(netCred);
TfsClientCredentials tfsCred = new TfsClientCredentials(basicCred);
tfsCred.AllowInteractive = false;

TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri("https://tfs.gfi.pt:4430/tfs/gfi/"),tfsCred);

tpc.Authenticate(); // This one works, and when I enter tpc I can see I am correctly signed-in in TFS (I can for instance check my full name)

ITeamProjectCollectionService projCollect = tpc.GetService<ITeamProjectCollectionService>(); //returns null :(

I have 2 questions:

  1. What is the difference between the TfsConfigurationServer and the TfsTeamProjectCollection? I know the first leads me to the Server-Level and the second to the Collection-level, but, what is the server-level and collection-level?
  2. Why am I unable to get the projectCollection when I am perfectly capable of signing-in (why does the last line returns null when I am sure I have 2 projects on TFS)?

Upvotes: 1

Views: 550

Answers (2)

Giulio Vian
Giulio Vian

Reputation: 8343

Regarding question #1, you can find some whys in Introducing the TfsConnection, TfsConfigurationServer and TfsTeamProjectCollection Classes. Shortly, you can have many user databases, i.e. Collection, managed by a single TFS instance, i.e. the Server.

Upvotes: 1

Luis Gouveia
Luis Gouveia

Reputation: 8925

I just found out what the problem was. I just needed to replace the last line:

ITeamProjectCollectionService projCollect = tpc.GetService<ITeamProjectCollectionService>(); //returns null :(

By:

// Get the catalog of team project collections
ReadOnlyCollection<CatalogNode> collectionNodes = tpc.CatalogNode.QueryChildren(
new [] { CatalogResourceTypes.TeamProject},
false, CatalogQueryOptions.None);

// List the team project collections
foreach (CatalogNode collectionNode in collectionNodes)
{
    Console.WriteLine(collectionNode.Resource.DisplayName);
}

Upvotes: 1

Related Questions