TFS API Part 17: Get WorkSpaces Information

14/03/2009

2 comments

TFS API Part 17: Get WorkSpaces Information

In this post I’ll show how to obtain local workspaces information.

Download Demo

.image

First add reference for
     Microsoft.TeamFoundation
     Microsoft.TeamFoundation.Client
     Microsoft.TeamFoundation.Common.dll
     Microsoft.TeamFoundation.VersionControl.Client.dll

located in – C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\

Step 1 – Connect TFS + Create VersionControlServer Object

First I create a Domain Project Picker to get Team Foundation Server instance, and after getting tfs instance create new object type VersionControlServer.

DomainProjectPicker dp = new DomainProjectPicker(DomainProjectPickerMode.None);

dp.ShowDialog();

if (dp.SelectedServer != null)

{

    tfs = dp.SelectedServer;

    sourceControl = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));

    AddWorkSapces();

}

Step 2 – Get all workspace from local machine.

private void AddWorkSapces()

{

    Workspace[] wss = GetWorkspaces();

 

    foreach (Workspace ws in wss)

    {

        WorkSpaceItem item = new WorkSpaceItem(ws.Name, ws);

        comboBox1.Items.Add(item);

    }

}

* Create WorkSpaceItem

public class WorkSpaceItem

{

    public string Name { get; set; }

    public Workspace WorkSpace { get; set; }

 

    public WorkSpaceItem(string name, Workspace workspace)

    {

        this.Name = name;

        this.WorkSpace = workspace;

    }

    public override string ToString()

    {

        return this.Name;

    }

}

public Workspace[] GetWorkspaces()

{

    try

    {

        return sourceControl.QueryWorkspaces(null, sourceControl.AuthenticatedUser, System.Net.Dns.GetHostName().ToString());

        //You also can get remote workspaces

        //QueryWorkspaces(worspaceName,worksapceOwner,computer)

    }

    catch

    {

        throw;

    }

}

Step 3 – Display workspace details.

if (comboBox1.SelectedItem != null)

{

    WorkSpaceItem item = (WorkSpaceItem)comboBox1.SelectedItem;

    info.Text = Convert.ToString(item.WorkSpace);

}

 

Add comment
facebook linkedin twitter email

Leave a Reply

2 comments

  1. Rajesh07/04/2012 ב 13:36

    Thanks for these great articles! Just curious to know how did you add that “Send Feedback” link to title bar?

  2. Turner26/07/2012 ב 09:28

    Thank you, I have recently been looking for details about this subject for ages and yours is that the
    best I have found so far.