TFS API Part 22 – Create Link Between Work Item (Parent, Child etc…)

27/02/2010

TFS API Part 22 – Create Link Between Work Item (Parent, Child etc…)

I got couple of question regarding Custom Link Types in TFS 2010, as you know one of the most important features in 2010 is new Link and Hierarchy capability.

In this post I’ll show and give a demo application on how to create custom link type through TFS API.

image

Download Demo Project

Step 1: Create Project and Add Reference

Create an WPF/WinForm application and add the following references:

     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 2: Connect to Team Foundation Server

private WorkItemStore store;

private TfsTeamProjectCollection tfs; – As you can see TeamFoundationServer is obsolete –

TFS API Part 20: Bye TeamFoundationServer and Welcome TfsTeamProjectCollection 

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

pp.ShowDialog();

 

if (pp.SelectedTeamProjectCollection != null)

{

    this.tfs = pp.SelectedTeamProjectCollection;

    pp.SelectedTeamProjectCollection.EnsureAuthenticated();

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

    GetLinkTypes();

    ActiveForm();

}

 

Step 3: Get Link Types From WorkItemStore
image

Using WorkItemStore object you can take all Link Types from TFS, I created a new object to contain all the information in LinkTypeItem.

void GetLinkTypes()

{

    foreach (WorkItemLinkType type in store.WorkItemLinkTypes)

    {

        combo_types.Items.Add(new LinkTypeItem(type));

    }

}

 

public LinkTypeItem(WorkItemLinkType type)

{

    this.CanDelete = type.CanDelete;

    this.CanEdit = type.CanEdit;

    this.ForwardEnd = type.ForwardEnd;

    this.IsActive = type.IsActive;

    this.IsDirectional = type.IsDirectional;

    this.IsNonCircular = type.IsNonCircular;

    this.IsOneToMany = type.IsOneToMany;

    this.LinkTopology = type.LinkTopology;

    this.ReferenceName = type.ReferenceName;

    this.ReverseEnd = type.ReverseEnd;

}

 

Step 4: Create Link Between Work Items

image

try

{

    LinkTypeItem type = combo_types.SelectedItem as LinkTypeItem;

    int from_wit = Convert.ToInt32(txt_wit_from.Text);

    int to_wit = Convert.ToInt32(txt_wit_to.Text);

 

    WorkItem from = store.GetWorkItem(from_wit);

 

    //Define what type of link to add.

    //Child, Parent etc…

    WorkItemLinkTypeEnd linkTypeEnd = store.WorkItemLinkTypes.LinkTypeEnds[type.ReverseEnd.Name];

    //Add the link as related link.

    from.Links.Add(new RelatedLink(linkTypeEnd, to_wit));

    from.Save();

 

    MessageBox.Show(string.Format("Work Item {0}, is {1} of Work Item {2}",

        from_wit, type.ReverseEnd.Name, to_wit),

        "Relation Saved",MessageBoxButton.OK,MessageBoxImage.Information);

}

catch (Exception ex)

{

    MessageBox.Show(ex.Message,"Cannot Save Work Item Relation",MessageBoxButton.OK,

        MessageBoxImage.Error);

}

 

Using Demo Application:

Open demo application select the desire link type and write two work item numbers: From work item and To work item ids.

After you click the “Create Link” button you should see message box with the result.

image

Also you should check in Team System:

image

 

In the pictures above I’ve used Hierarchy link type between work item 8 and 12, when trying to create circular relationships I got the following error, read more about Link Types: TFS 2010 Work Item Link Types

 

image

Download Demo Project

Enjoy

Add comment
facebook linkedin twitter email

Leave a Reply

6 comments

  1. parky1525/03/2010 ב 04:06

    Hi~~shai~~
    Would you try to edit link type with api?
    We have some problem with using copy method.
    We want to change link type to ‘Tree’ after copy from another workitem.
    But api supply only ‘Get’ property at Link type in workItem object.
    Have you ever tried?

  2. Stephen03/10/2010 ב 19:25

    Solid post! This will be very helpful to me. The TFS Integration Platform cannot setup Hierarchy. This example will help me to do that after we migrate.

    Just one comment. Either I am confused on what a “Parent” is (which is quite possible) or the image you have above is backwards. I have a Sprint Backlog Item as what I would think of as the “Parent” and Task that should be the child. I put the number for the Sprint Backlog Item in the “From:” box and the Task in the “To:” box. This set the Task up as owning the Sprint Backlog Item.

    I switched them and it setup the link correctly (it showed up in the list of “Tasks” on the Sprint Backlog Item).

    Either way, thank you for the great code! It will be a big help!

  3. Asif20/06/2011 ב 00:10

    Is there a way to define custom properties to existing or custom links?

  4. Walter11/04/2012 ב 21:11

    Hi. How can I delete some Link between two WI?
    Thx!

  5. shair13/04/2012 ב 10:15

    Instead of Add method use Remove and give the link object you wish to remove.

  6. Prashant11/10/2012 ב 07:58

    Thanks a lot for the information ..