Click here to Skip to main content
15,886,069 members
Articles / Desktop Programming / Windows Forms

Connecting to Team Foundation Server 2008

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
9 Sep 2009CPOL2 min read 23.9K   374   9   2
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:

C#
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.

C#
/// <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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO Servicelayer
Norway Norway
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionApplication error Pin
Member 102544015-Sep-13 3:51
Member 102544015-Sep-13 3:51 
AnswerRe: Application error Pin
David Schiffer6-Oct-13 5:20
professionalDavid Schiffer6-Oct-13 5:20 
Yes, you forgot to reference the Microsoft.TeamFoundation.Client assembly like so:
http://msdn.microsoft.com/en-us/library/vstudio/wkze6zky.aspx[^]

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.