Connect to TFS server and get workitems related to projects using C#.net

Hello everyone, In this post we are going to take a look at an interesting topic about Team Foundation Server and how to get the project information from the server.

What is a Team Foundation Server?
Team Foundation Server (TFS) is a Microsoft Product offering for source control, data collection, reporting and project tracking. It is intended for collaborative software development project. It is available as stand-alone software, or as server side back end platform for visual studio team system (VSTS). To know more about it please click here.

While we do all collaborative software development using the team foundation server, we might need to do some kind of analysis with the data available in Team Foundation server. In order to do some analysis we might need to pull the data from TFS server into some database or other location. Let us see an example of connecting to the TFS server using the C#.net and display all the work items associated to each project created in TFS.

Following are the assemblies we need add reference in the project
1. Microsoft.TeamFoundation
2. Microsoft.TeamFoundation.Client
3. Microsoft.TeamFoundation.WorkItemTracking.Client

Below mentioned is the source code which is used to pull the data from the TFS Server.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.Server;

namespace ConnectToTFS
{
class ConnectToTFS
{
static void Main(string[] args)
{

TfsTeamProjectCollection projCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("<a href="http://server:port/vpath">http://server:port/vpath</a>"));
ICommonStructureService Iss = (ICommonStructureService)projCollection.GetService(typeof(ICommonStructureService));
ProjectInfo[] ProjInfo = Iss.ListProjects();
WorkItemStore WIS = (WorkItemStore)projCollection.GetService();
foreach (ProjectInfo pi in ProjInfo)
{

 WorkItemCollection WIC = WIS.Query(
 " SELECT [System.Id], [System.WorkItemType]," +
 " [System.State], [System.AssignedTo], [System.Title] " +
 " FROM WorkItems " +
 " WHERE [System.TeamProject] = '" + pi.Name +
 "' ORDER BY [System.WorkItemType], [System.Id]");

 foreach (WorkItem wi in WIC)
 {
 Console.WriteLine(wi.Id);
 Console.WriteLine(wi.Title);
 }
}

}

}

}

Replace (http://server:port/vpath) with appropriate TFS Server URI and run the code, you will get work item related information for each project. We can insert the data into database or to other files and do analysis.
In this example we have only displayed id and title of the work item, if you want additional details of the work item, please refer to the properties of the work items here and add those properties to the code to get the required information.

One thought on “Connect to TFS server and get workitems related to projects using C#.net

  1. Nice work , Many thanks for sharing your knowledge.
    but there should be a small change in one code line like that

    WorkItemStore WIS = (WorkItemStore)projCollection.GetService(typeof(WorkItemStore));

Leave a comment