TFS API Part 21: Delete Work Item using WorkItemStore (Demo Application)

19/02/2010

TFS API Part 21: Delete Work Item using WorkItemStore

Over the last weeks I heard couple of times a fast way to delete work items from Visual Studio 2010, so in this post I’ll show how to build a simple Delete Work Item application using WorkItemStore.

*** Deleting Work Item Action Is Not Recoverable ***

image 

Download Demo Project

Create WinForm or WPF application project and add the following reference:

First add reference for
     Microsoft.TeamFoundation.WorkItemTracking.Client.dll
(C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.TeamFoundation.WorkItemTracking.Client.dll)
     Microsoft.TeamFoundation.Client.dll
(C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.TeamFoundation.Client.dll)

Step 1: Connect to Server

TeamProjectPicker pp = new TeamProjectPicker(TeamProjectPickerMode.SingleProject, false);

    pp.ShowDialog();

 

    if (pp.SelectedTeamProjectCollection != null)

    {

        pp.SelectedTeamProjectCollection.EnsureAuthenticated();

        store = (WorkItemStore)pp.SelectedTeamProjectCollection.GetService(typeof(WorkItemStore));

    }

Step 2: Delete Work Item

As you can see 2010 API supports work item deletion, calling WorkItemStore object will allow you using DestroyWorkItems method.

IEnumerable<WorkItemOperationError> DeleteWorkItem(int[] ids)

{

    try

    {

        return store.DestroyWorkItems(ids);

    }

    catch (Exception ex)

    {

        MessageBox.Show(ex.Message);

        return null;

    }

}

Step 3: Check For Errors

DestroyWorkItems will return a IEnumerable<WorkItemOperationError>, when everything is OK the collection will be empty.

void DeleteStatus(IEnumerable<WorkItemOperationError> enumerable)

{

    List<WorkItemOperationError> list = new List<WorkItemOperationError>(enumerable);

    if (list.Count > 0)

    {

        StringBuilder builder = new StringBuilder();

        for (int j = 0; j < list.Count; j++)

        {

            builder.AppendLine(string.Format("Work Item Id:{0}, {1}", list[j].Id, list[j].Exception.Message));

        }

        MessageBox.Show(builder.ToString(),"Error Deleting Work Item",MessageBoxButton.OK,MessageBoxImage.Error);

    }

    else

    {

        MessageBox.Show("Done");

        txt_id.Text = string.Empty;

    }

}

Full Code:

private void btn_server_Click(object sender, RoutedEventArgs e)

{

    btn_delete.IsEnabled = false;

    TeamProjectPicker pp = new TeamProjectPicker(TeamProjectPickerMode.SingleProject, false);

    pp.ShowDialog();

 

    if (pp.SelectedTeamProjectCollection != null)

    {

        pp.SelectedTeamProjectCollection.EnsureAuthenticated();

        store = (WorkItemStore)pp.SelectedTeamProjectCollection.GetService(typeof(WorkItemStore));

        btn_delete.IsEnabled = true;

    }

}

 

IEnumerable<WorkItemOperationError> DeleteWorkItem(int[] ids)

{

    try

    {

        return store.DestroyWorkItems(ids);

    }

    catch (Exception ex)

    {

        MessageBox.Show(ex.Message);

        return null;

    }

}

 

void DeleteStatus(IEnumerable<WorkItemOperationError> enumerable)

{

    List<WorkItemOperationError> list = new List<WorkItemOperationError>(enumerable);

    if (list.Count > 0)

    {

        StringBuilder builder = new StringBuilder();

        for (int j = 0; j < list.Count; j++)

        {

            builder.AppendLine(string.Format("Work Item Id:{0}, {1}", list[j].Id, list[j].Exception.Message));

        }

        MessageBox.Show(builder.ToString(), "Error Deleting Work Item", MessageBoxButton.OK, MessageBoxImage.Error);

    }

    else

    {

        MessageBox.Show("Done");

        txt_id.Text = string.Empty;

    }

}

 

private void btn_delete_Click(object sender, RoutedEventArgs e)

{

    if (!string.IsNullOrEmpty(txt_id.Text) && IsInteger(txt_id.Text))

    {

        IEnumerable<WorkItemOperationError> enumerable;

        int[] ids = new int[] { Convert.ToInt32(txt_id.Text) };

        enumerable = DeleteWorkItem(ids);

        DeleteStatus(enumerable);

    }

}

 

public static bool IsInteger(string theValue)

{

    try

    {

        Convert.ToInt32(theValue);

        return true;

    }

    catch

    {

        return false;

    }

}

image 

Download Demo Project

Add comment
facebook linkedin twitter email

Leave a Reply

4 comments

  1. HTH26/02/2010 ב 23:57

    excellent series, good work Keep it up

  2. George Gomez29/09/2010 ב 19:49

    Thank you for the informative article. Quick Question:

    How would you create a New TFS Item and return it’s item number after it is created.

    Thank you,

    George

  3. Tony02/02/2011 ב 10:02

    Thanx a lot……this app was needed
    Don’t know why didnt microsoft gave this feature ??