How to connect to TFS from C#

At one point in time I needed some reports from TFS related to the work items that were handled during some sprints. Even though this is possible from Excel (there is a nice integration between these two) my developer ego was hurt by this ideea and I’ve decided to create a little app in C# that connects to TFS. I’ve searched high and I’ve seached low but did not find any full tutorial or at least a working one so the next step is to share with you how you can achieve this.

Step 1 – create a new Visual Studio project

First thing, first thing first, create a new project inside VS (I used VS 2013 since 2015 was not yet released). Initially, for POC purposes, the application was created as desktop application with all the relevant code in it. After few successful tests I’ve moved the code handling TFS communication into a BL and added a new ASP.Net project which acts as the GUI. The Desktop app still works but I’m not planning of using it anymore.

Step 2 – Create the main UI

For the GUI part I’ve decided to use the ASP.Net project type since I’m very familiar with it and because it provides all the tools to create a simple web application very fast (in my humble opinion). After cleaning all the junk that Visual Studio creates (authentications, authorizations, account managing pages, etc.) the only files that remained were default.aspx, global.asax, web.config and Site.Master.

Step 3 – Create a framework for generating reports

Since the reports will be either seen on a web page or sent by email and because theoretically the reports can also be generated by a desktop application, I’ve chosen to put the logic of generating the reports in the ReportingBL and generate them using Razor. Since Razor was designed to work with Web thingies and because the ReportingBL could be used by multiple clients without Web thingies I’ve downloaded a small set of classes from http://www.getcodesamples.com/src/EF734693 which handle the runtime compiling of my templates.

There could be a package you can download from somewhere but I did not bother to find it. This little gem of classes let’s you write code as below but I’m sure you already know about how, why, etc. If not, read here.

Step 4 – Connect to TFS from your app

This is done using the following snippet (I’m not including the variable definition because you can find them easily)