Connect TFS Team Project through C#

In order to begin, first you need to create a C# class in your project and add the following code:

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.TestManagement.Client;

If you want to connect to the TFS Team Project with hard coded values you can use the following code:

Uri tfsUri = new Uri(http:mytfsuri.comteamCollection);
string teamProjectName = TestProject;
TfsTeamProjectCollection myTfsTeamProjectCollection = new TfsTeamProjectCollection(tfsUri);

First you initialize new URI object with your team collection URL which is equal to your TFS URL plus your team collection name. After that, you should create a collection object and initialize it with the URI. You use “GetService” method to initialize the TFS service. Then you can get the team project from it.
Connect to TFS Team Project using TeamProjectPicker Dialog

Collect TFS Settings with TeamProjectPicker Dialog

In order to capture the needed connection information, you can add TeamProjectPicker object to your windows app. You can use its method “ShowDialog” in order to collect the information.

using (projectPicker)
{
var userSelected = projectPicker.ShowDialog();
if (userSelected == DialogResult.Cancel)
{
return;
}
if (projectPicker.SelectedTeamProjectCollection != null)
{
Uri tfsUri = = projectPicker.SelectedTeamProjectCollection.Uri;
string teamProjectName = projectPicker.SelectedProjects[0].Name;
TfsTeamProjectCollection myTfsTeamProjectCollection = projectPicker.SelectedTeamProjectCollection;
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s