Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Connecting to Team Foundation Server 2008

0.00/5 (No votes)
9 Sep 2009 1  
A simple WinForms application that lets you connect to Team Foundation Server 2008.

Introduction

I am writing this article because I saw the need to be able to connect applications other then Visual Studio to the Team Foundation Server. Imagine an external resource editor which makes it easier for you to create and edit resources in Visual Studio. Now, without a connection to your Team Foundation Server, any edits to files in your solution will get synchronized with your team.

We need to download the Visual Studio SDK to learn more. The libraries needed are downloadable with the source code I have added for you.

This is a simple WinForms application doing nothing more than assisting in creating a connection to your Team Foundation Server.

Background

I had downloaded the Zeta Resource editor Uwe Keim created and found it to be a very useful tool for editing resources in Visual Studio. However, due to the fact that it is not connected to the Team Foundation Server, the Team Foundation Server does not know about the changes made and check in's won't upload the updates to the server. He did not know about this, and asked me how to connect to the sever, so here it goes:

Using the Code

Let’s get started.

First, we need to create a form with a few textboxes and a menu to connect, disconnect, and to close the form. The textboxes need to contain information like username and password as well as the name of the domain your Team Foundation Server resides in. A few more parameters are needed like the port number as well as what protocol we want to connect with (HTTP or HTTPS).

Last but not least, we also need the name of your team server, like so:

Windows form

The first thing we want to do now is to write the event handler behind the Connect menu item:

private void connectToolStripMenuItem_Click(object sender, EventArgs e)
{
    try
    {
        _teamFoundationServer = TeamFoundationServer();
        _teamFoundationServer.Authenticate();
        if (_teamFoundationServer.HasAuthenticated)
        {
            toolStripStatusLabel.Text = "Connected to: " + _teamFoundationServer.Name;
            return;
        }
        throw new Exception("Failed to Authenticate for: " + _teamFoundationServer.Name);
    }
    catch (Exception exp)
    {
        toolStripStatusLabel.Text = "Failed to connect to: " + 
                                    CreateTeamserverUrl(txtTfs.Text);
        MessageBox.Show(exp.Message);
    }

}

Helper methods

The CreateTeamserverUrl method creates a fully qualified URL to the server.

That done, we need to create network credentials allowing the connection. We create a new instance of the NetworkCredential object, passing in our username, password, and the domain name the Team Foundation Server's security is managed by.

Now we can create an instance of the TeamFoundationServer object, by passing in the network credentials and the fully qualified URL to the server: the TeamFoundationServer object is in the Microsoft.TeamFoundation.Client namespace.

Alternate ways to create an instance would be using the TeamFoundationServerFactory.GetServer(string url) method which also will return an instance of the TeamFoundationServer object.

The TeamFoundationServer class, its methods, and events are described in the MSDN article: http://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.client.teamfoundationserver(VS.80).aspx.

/// <summary>
/// Creating an Instance of the TeamFoundationServer Object
/// </summary>
/// <returns>An Instance of the TeamFoundationServer Object</returns>
private TeamFoundationServer TeamFoundationServer()
{
    // Get a reference to our Team Foundation Server. 
    String tfsUrl = CreateTeamserverUrl(txtTfs.Text);
    string userName = txtUsername.Text;
    string password = txtPassword.Text;
    string domain = txtDomain.Text;
    var nwCred = new NetworkCredential(userName, password, domain);
    return new TeamFoundationServer(tfsUrl, nwCred);
}
/// <summary>
/// Created a fully qualified url to the teamfoundation server based on the users input
/// </summary>
/// <param name="server">The name of the teamfoundation server</param>
/// <returns>The fully qualified url to the teamfoundation server</returns>
private string CreateTeamserverUrl(string server)
{
    var retValue = "http://";
    if (rbHttps.Checked)
    {
        retValue = "https://";
    }
    retValue += server + ":" + txtPort.Text;
    return retValue;
}

It's as simple as that, all you need to do now is to run the application, type in the real values to connect to your server, and we are good to go.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here