DCSIMG
TFS API Part 21: Delete Work Item using WorkItemStore (Demo Application) - Shai Raiten's Blog

Shai Raiten's Blog

It's all about code...

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

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

Comments

HTH said:

excellent series, good work Keep it up

# February 26, 2010 11:57 PM

George Gomez said:

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

# September 29, 2010 7:49 PM

shair said:

Hi,

You need to use WorkItemStore object and use the Create Method it automatically return the new ID.

blogs.microsoft.co.il/.../tfs-api-part-5-workitemstore-get-project-details-workitemtypes-queries.aspx

# October 5, 2010 10:48 AM

TFS 2010 Work Item Seed: TFS Work Item system.id at a predefined number | Pozitive.NeT said:

Pingback from  TFS 2010 Work Item Seed: TFS Work Item system.id at a predefined number | Pozitive.NeT

# January 31, 2011 2:43 AM

Tony said:

Thanx a lot......this app was needed

Don't know why didnt microsoft gave this feature ??

# February 2, 2011 10:02 AM

TFS 2010 Work Item Seed: TFS Work Item system.id at a predefined number « Visual Studio ALM said:

Pingback from  TFS 2010 Work Item Seed: TFS Work Item system.id at a predefined number &laquo;  Visual Studio ALM

# May 24, 2011 9:33 AM

Programatically delete [permenantly] a TFS work item - Programmers Goodies said:

Pingback from  Programatically delete [permenantly] a TFS work item - Programmers Goodies

# October 3, 2011 3:50 PM

Shai Raiten said:

In previous post - TFS API Part 21: Delete Work Item using WorkItemStore (Demo Application) I showed

# April 6, 2012 1:28 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: