Getting started with the TFS API (Get Latest App)

by David Kiff 2. July 2009 23:02

Introduction

I have been working for around two years within the In-store team for Tesco.com. Around three weeks ago, I was asked to help the web team deliver the customer’s favourite’s part of the new Tesco.com website, code named Martini.

After the first day when I performed a "get latest" and then on to a full build, I realised I needed some automation.

The get latest took up to one hour and 30mins for the build (it’s quite a large site!). I realise I could use MS Build or NAnt to perform the get latest and build; however I really thought I should be looking at something new.

With this thought, I decided to learn some of the TFS API and found it extremely straightforward and easy to use!

I decided the application should have the following three basic requirements:

  • Be runnable from the command line (with no GUI) so that it can be scheduled and run invisibly from within Windows Task Scheduler
  • Able to display a GUI so users can easily get latest
  • Allow the user to specify the arguments required, such as Workspace, Server etc

Getting started

Before diving straight into development, there are a few prerequisites. We need to install the latest Microsoft Visual Studio 2008 SDK (currently version 1.1).

This is currently available at http://www.microsoft.com/downloads/details.aspx?familyid=59EC6EC3-4273-48A3-BA25-DC925A45584D&displaylang=en.

Setting up the project

In order to get started, I created a new Windows Forms Application. Firstly we need to add references to the TFS dlls, which were installed as part of the SDK.

To do this, right click on references, from within the Solution Explorer and select "Add Reference". Select the Browse tab and go to "C:\Program Files\Microsoft Visual Studio 2008 SDK\VisualStudioIntegration\Common\Assemblies" if you have installed the SDK in the default place.

For this example, we only need to add two references:

  1. Microsoft.TeamFoundation.Client.dll
  2. Microsoft.TeamFoundation.VersionControl.Client

That should be ready to setup and start coding!

Coding..

Now for the best part.. the development. We will start with the code required to interact with TFS (TFSSourceControl class).

Firstly create a new class named TFSSourceControl (or something better if you have more imagination!) and add a new GetLatest() method.

The method should take the workspace, owner and machine name, so follow this signature:

public void GetLatest(string workspace, string owner, string machine)

Before we can interact with TFS, we need to create an instance of it! There are two main ways to do this, through a factory method, or via the constructor:

private TeamFoundationServer _tfs
public void SetServer(string tfsServerHostName)
{
   _tfs = TeamFoundationServerFactory.GetServer(tfsServerHostName);
   _tfs = new TeamFoundationServer(tfsServerHostName);
}

We obviously don’t need to use both, however I thought it would be a good idea to show you anyway! Please delete one of the lines within the SetServer() method shown above.

Now we have an instance of TFS we can use the service locator pattern to create an instance of the Version Control Server.

VersionControlServer versionControlServer = (VersionControlServer)_tfs.GetService(typeof(VersionControlServer));

We can use the version control server to query our workspaces, passing in the workspace that requires the get latest, the owner of the workspace (usually your domain username with domain prefix) and the machine name where the code will reside locally.

When we have the workspace we can perform the get latest by calling the Get() method on it, passing the version specification and get options.

Workspace[] workSpaces = versionControlServer.QueryWorkspaces(workspace, owner, machine);
if (workSpaces.Length > 0)
{
   workSpaces[0].Get(VersionSpec.Latest, GetOptions.GetAll);
}

That is the core of the application; the rest is simply plumbing in order to pass the parameters down that method. All of the code (including the plumbing) is available to download and use as you wish, however please note this application was written whilst waiting for the manual get latest and build to occur, so it is not of the utter most highest quality.

When running the GUI based mode, there is a need to implement very basic threading in order for the GUI not to lock up.

Disclaimer

I am not responsible for any adverse affects from running this application and it is your entire responsibility, should you wish to. I would highly recommend checking through the code before running it to ensure you are completely satisfied.

 Download: DavidKiff.TFS.GetLatest.zip (56.38 kb)

Tags:

TFS

Comments

10/4/2009 11:56:00 PM #

Интернет Маркетинг

I can see that you are putting a lot of time and effort into your blog and detailed articles! I am deeply in love with every single piece of information you post here (there are not many quality blogs leftSmile.
Regards,
Nick

Интернет Маркетинг