TFS API Part 22 – Create Link Between Work Item (Parent, Child etc…)
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.

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 –
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
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
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.
Also you should check in Team System:

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

Download Demo Project
Enjoy