Click here to Skip to main content
Click here to Skip to main content

Working with TFS Server

By , 23 Jun 2007
 

Introduction

I've worked on a project that must have the ability to integrate with Team Foundation Server. I tried to search CodeProject, but unfortunately found only one article concerning this. I was quite confused about the topic, but got it all clear - there is a very good documentation of TFS programming in MSDN. So, in my post, I'll outline links to the source information and will present a short piece of code for novice developers.

Before starting

  • Ensure that you have installed Visual Studio 2005 SDK (see links below).
  • The simplest way is to use GAC.

  • Add references to the TeamFoundation assemblies.
  • Screenshot - TFS.gif

Working with TFS

This is an example of how to connect to a TFS server, and retrieve Projects and their WorkItems using WIQL (WorkItem Query Language, see links):

//
// Using Section
// 

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client; 


public void WorkWithTFS(string login, string password, string tfsName)
{
    //Connecting Server
    NetworkCredential tfsCredential = new NetworkCredential(login, password);
    TeamFoundationServer tfs = new TeamFoundationServer(tfsName, tfsCredential);
    tfs.Authenticate();

    WorkItemStore wis = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));

    //Iterate Through Projects
    foreach (Project tfs_project in wis.Projects)
    {
       Console.WriteLine(tfs_project.Name);

       //Perform WIQL Query 
       WorkItemCollection wic = wis.Query(
          " SELECT [System.Id], [System.WorkItemType],"+
          " [System.State], [System.AssignedTo], [System.Title] "+
          " FROM WorkItems " + 
          " WHERE [System.TeamProject] = '" + tfs_project.Name + 
          "' ORDER BY [System.WorkItemType], [System.Id]");
       foreach (WorkItem wi in wic)
       {
         Console.WriteLine(wi.Title + "["+wi.Type.Name+"]"+wi.Description);
       }
     }
}

You can also use Queries stored on the TFS server to retrieve WorkItems.

Also, the TFS SDK allows you to completely manage the TFS Server; for more information search MSDN.

Links

History

  • 23 June 2007: First version.

License

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

About the Author

Petro Protsyk
Architect
Netherlands Netherlands
Member
Please visit my website for more articles

I'm software developer & Ph.D. student

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralThanks, concise and to the point.memberedger27 Apr '10 - 17:40 
I just copied the code over to VS2010 and it works.
a little update for vs2010 and .Net 4. You need to use the new TfsTeamProjectCollection class and hence Uri for tfsName rather than string.
 
public void WorkWithTFS(string login, string password, Uri tfsName)
{
//Connecting Server
 
NetworkCredential tfsCredential = new NetworkCredential(login, password);
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(tfsName, tfsCredential);
tfs.Authenticate();

//...
}

GeneralCannot pass a GCHandle across AppDomains.membernaveensri23 Apr '08 - 0:16 
Hi,
 
I am getting the following exception
 "Cannot pass a GCHandle across AppDomains." 
while the following line was getting executed
WorkItemStore wis = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));
 
can you please help me in this regard.

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 23 Jun 2007
Article Copyright 2007 by Petro Protsyk
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid