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 ***
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;
}
}
Download Demo Project