How To: Create Generic Work Item Migration Tool For TFS

06/10/2008

How To: Create Generic Work Item Migration Tool For TFSMission_Migration_Screen


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.migration

  • 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.

workItem.Save();

Use Open method to active the Work Item for edit.


workItem.Open();

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);
}

Add comment
facebook linkedin twitter email

Leave a Reply

8 comments

  1. unlidgend09/10/2008 ב 13:48

    Thanks for the pillar, i dont unusually do much christmas shopping but retard further down, also i accept added you to RSS gratify detain pillaring :)

    http://xmasprezzies.blogsome.com/

  2. Adar27/11/2008 ב 03:28

    Very very helpful, thanks!!
    This is the kind of things that are so trivial when you know them, but seem so difficult when you don’t :-)

    A small addition:
    Since we not always have privileges to customize the Work Item Definition, the first save might still fail.

    Solution (got it also from Shai, of course :) ):
    The validation process can be used also here, to help one understand which fields are still missing, and add them as well before sending. It is very helpful in migration, and also in just opening bugs using this API.
    Notice, that the validation failure and allowed values are stored in the work item field object. So when validation fails, they can be used to indicate what is missing, and what the possible values can be.

  3. Shai Raiten27/11/2008 ב 03:45

    Thanks for the comment.
    It’s my pleasure.
    If you got any other question please let me know.
    Thanks

  4. wow power leveling16/04/2009 ב 09:53

    A wonderful article…. In my life, I have never seen a man be so selfless in helping others around him to get along and get working.

  5. cypressx21/07/2009 ב 18:02

    I rarely leave comments, but this article is just great.
    Thanks for sharing your knowledge with everyone!

  6. R405/02/2010 ב 10:45

    Excellently written article, if only all bloggers offered the same content as you, the internet would be a much better place. Please keep it up!

  7. r428/02/2010 ב 15:25

    Really like how this article was layed out, I shall be coming back to your site soon :)