How To: Create Generic Work Item Migration Tool For TFS
How To: Create Generic Work Item Migration Tool For TFS
I heard people creates Migration Script with tfs command-line tools but in this tutorial I will show how to create Generic Migration Tool using TFS API.
Preparing For Migration
Before starting the Migration Tool you need to customize the Work Item Definition so you will not have problems during the Migration.
For Example:
- Remove or Modify <REQUIRED> tags to make sure you will not have validation errors.
- Make sure that the first transition is the one you want.
- You can only set one transition for a Work Item Definition.
- If you want to change the Work Item status you will need to save between transitions.

- Make sure the Users names are equal to the Users Name in TFS
- You can remove the <VALIDUSER> from AssignTo field definition if you don't want to compare User Names.
- Make sure that fields or global list have the values your about to add.
- You can replace <ALLOWEDVALUES> with <SUGGESTEDVALUES> to allow any value.
Create Work Item with TFS API
How to create Work Item using API?
First create your project and add the following reference:
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
Connect to TFS to get Work Item store and define WorkitemTypeCollection.
TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer(ServerName);
WorkItemStore store = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));
WorkItemTypeCollection workItemTypes = store.Projects[TFSProject].WorkItemTypes;
Define Work Item Type and Create new Work Item
WorkItem workItem = new WorkItem(workItemTypes["Bug"]);
Why saving the Work Item now?
- If your Work Item not only contains Core fields (System.Reason, System.Title, etc etc.) that you want to change,
you first need to save the Work Item so will be have access to those fields.
- If you only want to change Core fields will can save the Work Item after you finish change all the values you want.
Use Open method to active the Work Item for edit.
With Fields you can apply values to specific field.
workItem.Fields["CustomField.Star"].Value = "Five Stars";
DateTime date = Convert.ToDateTime("05/10/2008 22:35:00");
workItem.Fields["MyField.CustomDateField"].Value = date;
Add Hyper Link
Link most be valid!
Hyperlink hp = new Hyperlink("http://blogs.microsoft.co.il/blogs/shair");
hp.Comment = "Shai Raiten Blog";
workItem.Links.Add(hp);
Add Attachment
TFS will search for the file, so make sure he exists.
Attachment a = new Attachment(@"C:\FileToAdd.txt, "Comment....");
workItem.Attachments.Add(a);
Make sure your Work Item is Valid
After you finish adding all the wanted values into the Work Item make sure that all fields are Valid and your can save the Work Item, This step is to make sure that you prepare the Work Item Definition for the migration.
ArrayList invalidFields = new ArrayList();
foreach (Field field in workItem.Fields)
{
if (!field.IsValid)
{
invalidFields.Add(field);
Console.WriteLine("Invalid Field '{0}': {1}", field.Name, field.Status.ToString());
Console.WriteLine("Current Value: '{0}'", field.Value);
}
}
//There are some Invalid Fields.
if (invalidFields.Count > 0)
{
Console.WriteLine("Invalid Bug Track ID:{0}", invalidFields.ToArray());
return;
}
else
{
workItem.Save();
Console.WriteLine("Work Item #{0} Successfully Updated.Bug Track ID:{1}", workItem.Id.ToString(), OldItemID);
}