TFS API Part 49 – Delete Team Project

20/02/2013

There are many improvements in TFS API for 2012 version, in my previous post I’ve talked about TFS API Part 48 – WorkItemControl And Bypass Work Item Rules that allow work item creation with bypass rule mode in WorkItemStore that allow you to create and change any field value without applying the WorkItem template rules.

in this post I’ll show how to delete Team Project using TFS API, be aware Delete Team Project is unrecoverable!

image

Download Demo Project

Step 1: Connect TFS and Getting Team Projects

Create WPF Application, and add the basic code for connecting TFS and collecting all Team Projects under that collection.

private TfsTeamProjectCollection _tfs;
private WorkItemStore _store;
private void btnConnect_Click(object sender, RoutedEventArgs e)
{
    var tpp = new TeamProjectPicker(TeamProjectPickerMode.NoProject, false);
    tpp.ShowDialog();

    if (tpp.SelectedTeamProjectCollection != null)
    {
        _tfs = tpp.SelectedTeamProjectCollection;
        _store = new WorkItemStore(_tfs);
        GetTeamProjects();
    }
}

private void GetTeamProjects()
{
    projectsList.ItemsSource = _store.Projects;
}

Step 2: Using TeamProjectDeleter

To delete Team Project we need to use a new API called – TeamProjectDeleter, you can find this dll here – C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ReferenceAssemblies\v4.5\Microsoft.TeamFoundation.DeleteTeamProject.dll..

First create new TeamProjectDeleter object and pass the TfsCollection, Team Project name and force property.

You can also specify if you want to delete the Team Project WSS and finally calling the delete method to perform the Team Project delete operation.

private void btnDelete_Click(object sender, RoutedEventArgs e)
{
    if (projectsList.SelectedItem == null) return;

    var project = (Project)projectsList.SelectedItem;

    var force = chxForce.IsChecked == true ? true : false;
    try
    {
        var deleter = new TeamProjectDeleter(_tfs, project.Name, force);
        deleter.StatusEvent += deleter_StatusEvent;

        if (deleter.HasWssToDelete)
            deleter.ExcludeWss = chxExcludeWss.IsChecked == true ? true : false;

        var result = MessageBox.Show(string.Format("You are about to delete \"{0}\" project, are you sure you want to continue?",
            project.Name), "Delete Team Project", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
        if (result == MessageBoxResult.OK)
        {
            var t = new Thread(new ThreadStart(deleter.Delete));
            t.Start();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

void deleter_StatusEvent(object sender, TeamProjectDeleter.StatusEventArgs e)
{
    Execute ex = delegate()
    {
        txtLog.Text = e.message + Environment.NewLine + txtLog.Text;
    };
    this.Dispatcher.Invoke(ex);
}

Download Demo Project

Add comment
facebook linkedin twitter email

Leave a Reply