Click here to Skip to main content
15,885,904 members
Articles / Programming Languages / Visual Basic

Connect to TFS Team Project C# Code

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
22 Feb 2015CPOL2 min read 54.5K   14   4
Code examples how to connect to TFS Team Project using C# and VB .NET. The post - Connect to TFS Team Project C# Code appeared first on Automate The Planet.

Introduction

I will make a series of blog posts how to use the internal Microsoft Test Manager TFS API using C# code.

The first thing you have to do in order to use the Test Manager API is to add references to the Microsoft TFS DLLs. Most of them are available only after the installation of Visual Studio 2012/2013 and Microsoft Test Manager, which is included only in the Test, PRO, Premium and Ultimate versions of the product.

The default path to the most of the DLLs that you will need is the following: C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ReferenceAssemblies\v2.0\.

Most of them are available in the GAC but if you are going to use your application on a machine where the Visual Studio is not installed, probably you should consider the idea to create a NuGet package for the needed DLLs.

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

C#
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:

C#
Uri tfsUri = new Uri("http:\\mytfsuri.com\teamCollection");
string teamProjectName = "TestProject";
TfsTeamProjectCollection myTfsTeamProjectCollection = new TfsTeamProjectCollection(tfsUri);
ITestManagementService service = _
(ITestManagementService)myTfsTeamProjectCollection.GetService(typeof(ITestManagementService));
ITestManagementTeamProject myTestManagementTeamProject = _
		service.GetTeamProject(teamProjectName);

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 “GetServicemethod to initialize the TFS service. Then, you can get the team project from it.

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.

C#
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;
        ITestManagementService service = 
        (ITestManagementService)ExecutionContext.TfsTeamProjectCollection.GetService
		(typeof(ITestManagementService));
        ITestManagementTeamProject myTestManagementTeamProject = service.GetTeamProject(teamProjectName);
    }
}
VB.NET
Dim tfsUri As New Uri(@"http:\\mytfsuri.com\teamCollection")
Dim teamProjectName As String = "TestProject"
Dim myTfsTeamProjectCollection As New TfsTeamProjectCollection(tfsUri)
Dim service As ITestManagementService = 
DirectCast(myTfsTeamProjectCollection.GetService(GetType(ITestManagementService)), ITestManagementService)
Dim myTestManagementTeamProject As ITestManagementTeamProject = service.GetTeamProject(teamProjectName)

So Far in the TFS API Series

1. Connect to TFS Team Project C# Code
2. Manage TFS Test Plans C# Code
3. Manage TFS Test Cases C# Code
4. Manage TFS Test Suites C# Code
5. TFS Associate Automated Test with Test Case C# Code
6. Test Cases Statistics with SSRS and TFS Data Warehouse
7. SSRS SQL Server Reporting Services- Subscriptions for Reports

 

If you enjoy my publications, feel free to SUBSCRIBE
Also, hit these share buttons. Thank you!

Source Code

The post- Connect to TFS Team Project C# VB .NET Code appeared first on Automate The Planet.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO Automate The Planet
Bulgaria Bulgaria
CTO and Co-founder of Automate The Planet Ltd, inventor of BELLATRIX Test Automation Framework, author of "Design Patterns for High-Quality Automated Tests: High-Quality Test Attributes and Best Practices" in C# and Java. Nowadays, he leads a team of passionate engineers helping companies succeed with their test automation. Additionally, he consults companies and leads automated testing trainings, writes books, and gives conference talks. You can find him on LinkedIn every day.

Comments and Discussions

 
QuestionCan't download source files Pin
lakshman rao16-Jun-20 4:38
lakshman rao16-Jun-20 4:38 
QuestionAny luck with VS2013 and TFS2013, can't find any way to get TeamCollection Pin
Your Display Name Here8-Jun-15 13:02
Your Display Name Here8-Jun-15 13:02 
AnswerRe: Any luck with VS2013 and TFS2013, can't find any way to get TeamCollection Pin
Anton Angelov10-Jun-15 7:13
Anton Angelov10-Jun-15 7:13 
GeneralHow to Get Test Steps from VS2013 and TFS Pin
Your Display Name Here10-Jun-15 12:54
Your Display Name Here10-Jun-15 12:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.