TFS API Part 18: More Basic Stuff On Workspaces

22/03/2009

no comments

TFS API Part 18: More Basic Stuff On Workspaces

In my last post I showed how to TFS API Part 17: Get WorkSpaces Information

In this post I’ll show to go couple of basic actions on you workspace.

Demo Project

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 – Create Workspace

It’s a very simple action using tfs api, and can set folder and more workspace settings. (see comment)

private void btn_create_Click(object sender, RoutedEventArgs e)

{           

    sourceControl.CreateWorkspace(txt_name.Text);

    //sourceControl.CreateWorkspace(txt_name.Text, Environment.UserName);

    //sourceControl.CreateWorkspace(txt_name.Text, Environment.UserName, "Just Comment");

    //sourceControl.CreateWorkspace(txt_name.Text, Environment.UserName, "Just Comment", new WorkingFolder(serveritem,localitem));

    //sourceControl.CreateWorkspace(txt_name.Text, Environment.UserName, "Just Comment", new WorkingFolder(serveritem, localitem), "Computer Name");

    //sourceControl.CreateWorkspace(txt_name.Text, Environment.UserName, "Just Comment", new WorkingFolder(serveritem, localitem), "Computer Name",FixMappingErrors);

}

Step 4 – Delete Workspace

private void btn_delete_Click(object sender, RoutedEventArgs e)

{

    if (cob_workspace_list.SelectedItem != null)

    {

        WorkSpaceItem item = (WorkSpaceItem)cob_workspace_list.SelectedItem;

        item.WorkSpace.Delete();

 

        AddWorkSapces();

    }

}

Step 5 – Get Pending Changes

private void btn_GetPendingChanges_Click(object sender, RoutedEventArgs e)

{

    if (cob_workspace_list.SelectedItem != null)

    {

        WorkSpaceItem item = (WorkSpaceItem)cob_workspace_list.SelectedItem;

        PendingChange[] changes = item.WorkSpace.GetPendingChanges(); ;

 

        StringBuilder str = new StringBuilder();

        foreach (PendingChange change in changes)

        {

            str.AppendLine(string.Format("File name:{0},Change type:{1}",change.FileName,change.ChangeType.ToString()));

        }

        MessageBox.Show(str.ToString());

    }

}

 

See more action in the Demo Project

image

Add comment
facebook linkedin twitter email

Leave a Reply