Tag Archives: coding

TFS API: How to Get a Specific Version Programmatically

Within the Visual Studio Source Control Explorer, there are a number of options available for getting files out of TFS. Often times, developers just want to get the latest code to work with. But in some scenarios, developers need to get source files based on alternate criteria, such as by a specific date or label.  TFS provides a handful of options for selecting source files easily within the “Get Specific Version” dialog.

TFS: Get Specific Version Options

This works really well within Visual Studio, but how can you do this programmatically?  As it turns out, the Workspace.Get() method has an overloaded method that takes a VersionSpec argument.  Notice that VersionSpec is actually a base class. and that there are several inherited classes that can be used to specify the changeset type, including:

  • ChangesetVersionSpec
  • DateVersionSpec
  • LabelVersionSpec
  • LatestVersionSpec (equivalent to VersionSpec.Latest)
  • WorkspaceVersionSpec

For example, if you wanted to perform a GET on a specific changeset, you would simply pass an instance of ChangesetVersionSpec:

'connect to source control and get reference to existing workspace
Dim server As TfsTeamProjectCollection = _
   TfsTeamProjectCollectionFactory.GetTeamProjectCollection("http://myTfsServer:8080")
Dim vcs As VersionControlServer = _
   server.GetService(Of VersionControlServer)()
Dim ws As Workspace = vcs.GetWorkspace("MyExistingWorkspace", My.User.Name)

'Perform a GET on Changeset 100
Dim version As New ChangesetVersionSpec(100)
ws.Get(version, GetOptions.GetAll)

Performing a GET based on a pre-defined label works in much the same way (just replace the ChangesetVersionSpec object with a LabelVersionSpec object):

'connect to source control and get reference to existing workspace
' (same as above)...

'Perform a GET on items defined by label "My Label"
Dim version As New LabelVersionSpec("My Label")
ws.Get(version, GetOptions.GetAll)

Updated 2/28/2012: Revised code sample for TFS 2010 API.

Microsoft Windows and ASP.NET Tutorial Sites

I’ve been a fan of the Microsoft’s ASP.NET site for a while now.  If you’re not familiar with it, the site contains an awesome amount of information and resources for ASP.NET developers.  One of my favorite features of this site is the Learning section, which contains a large number of tutorial videos covering a range of different technologies, tools and controls within the realm of ASP.NET development.  While the videos are not usually in-depth on any single topic, they do provide great introductory examples to help me get past a lot of my initial hurdles and start developing something to cater to my specific needs.

Today, I came across theWindows Forms and WPF equivalent of the ASP.NET site, and I was amazed that I hadn’t thought to look sooner.  This site has the same great information and resources I was used to seeing for ASP.NET.  And this site also has a large number of video tutorials, both catering to the traditional Windows Forms developers as well as those moving into the WPF space.

This site has already made its way onto my list of useful work resources.  Today’s video was a review on how to use the BackgroundWorker class to simplify multi-threading.

TFS API: Check if Server Path Exists

Disclaimer:  Rarely do I get a chance to totally geek out and write a post specifically about code.  I enjoy the change-up.  And hopefully some of you will benefit from this.

Ok.  On a recent project, I’ve been playing around with the .NET APIs that were made available for interacting with TFS. You can tell that the documentation for these API’s are a little more raw than what is available for the .NET framework components, but that is not to say they aren’t still helpful.

As part of my project, I needed to programmatically download files to the local machine from version control.  As part of my unit testing, I wanted to validate the source path before attempting the download.  Basically, I wanted the equivalent of the System.IO.Directory.Exists() method in the .NET framework, but that validates against version control.

After several minutes of searching, I found the VersionControlServer.ServerItemExists() method. This handy method basically combines the functionality of the File.Exists() and Directory.Exists() methods, switching between the two (or combining) by setting the custom ItemType enumeration.

Using this method, I can first validate the path, as the following example shows:

Dim fakePath As String = "$/MyFakeTeamProject/RoadToNowhere"
Dim vcServer As VersionControlServer = Nothing

'...initialize a reference to the version control server here...

'Perform validation against server path before downloading.
' This example works for both file or directory paths.
If Not vcServer.ServerItemExists(fakePath, ItemType.Any) Then
   Throw New Exception("Hey! The path you gave me is bunk!")
End If

…and for the C# folks:

string fakePath = "$/MyFakeTeamProject/RoadToNowhere";
VersionControlServer vcServer = null;

//...initialize a reference to the version control server here...

// Perform validation against server path before downloading.
//  This example works for both file or directory paths.
if (!vcServer.ServerItemExists(fakePath, ItemType.Any))
{
   throw new Exception("Hey! The path you gave me is bunk!");
}

A Coder’s Challenge

I was recently “challenged” by a fellow agile member who claimed that Java developers have a higher maturity level then their fellow .NET developers.  His claim was that .NET developers rely too much on the mouse when programming, which makes them slower because their hands have to leave the keyboard more frequently.  Java developers, on the other hand, are more familiar with their tool (e.g. IDE) and all the keyboard shortcuts that are programmed into it.

So, I considered his claims and his challenge.  I scoured the interwebs, searching for the knowledge I sought that would help me master the .NET coder’s tool of choice (the great Visual Studio), until I found what I was looking for.

And so, for my fellow .NET “adolescents”, I share with you this, straight from our god herself:

You threw down the gauntlet, B.C. and I accept your challenge.

Anonymous Methods in C# 2.0

I love the anonymous methods feature that was added to C# 2.0. I got a taste of this feature in college when I used Java (which refers to them as anonymous inner classes). Regardless, this is yet another feature to help developers write cleaner code.

For example, let’s say your application has a button that, upon clicking, shows an “About” dialog, which displays various information about your application (e.g. version, logo, etc.). Upon closing that dialog, you’d like to display a message box, telling your user that they just saw your “About” dialog box (because users like having to click a lot…).

In C# 1.x, you’d have to write the following code to accomplish this:

private void Button1_Click(object sender, EventArgs e)
{
    AboutDialog dlg = new AboutDialog();
    dlg.Closed += new EventHandler(OnAboutDlgClosed);
    dlg.ShowDialog(this);
}

// separate method for handling the form close event.
private void OnAboutDlgClosed(object sender, EventArgs e)
{
    MessageBox.Show("You just saw my About dialog!");
}

However, in C# 2.0, you can do the same thing using anonymous methods:

private void Button1_Click(object sender, EventArgs e)
{
    AboutDialog dlg = new AboutDialog();
    // .NET 2.0 changed the name of the event to FormClosed
    dlg.FormClosed += delegate
    {
        MessageBox.Show("You just saw my About dialog!");
    }
    dlg.ShowDialog(this);
}

That’s it. Frickin’ sweet!

Ok, so there are a couple drawbacks that I’ve found while using anonymous methods. First, I’ve found that I cannot use the “Edit and Continue” feature in the Visual Studio 2005 Debugger. That is, I am not allowed to make a change in the method or anonymous method while stepping through the code — instead, I have to make my code change and restart the debugger. A minor nuisance, but still a nuisance.

The other potential issue is that, assuming the code in the anonymous method needs to be executed in response to multiple events, some developers may find themselves copying and pasting the code in several places of their application, thus ignoring the general rule of code re-use.

This second issue can be very subtle. It is a problem of design, and it is up to the developer to be on the lookout for it. In cases like this, the C# 1.x example would be a good solution that promotes code re-use.