If I do:
RelatedLink newLink = new RelatedLink(linkTypeEnd, id);
if (workItem.Links.Contains(newLink)) return;
workItem.Links.Add(newLink);
It still crashes on the Add method with a ValidationException, stating that the link is already in the collection.
TF237099: Duplicate work item link.
So what is the Contains really checking? reference equality? surely not?
Anyone got some tips on how to handle this? I'm writing a tool to migrate Requirements from a well known tool to TFS.
So you have a given WorkItem (let's assume with ID = 1000) & you want to add to it a related WorkItem (let's assume with ID = 1001).
Simply going for
workItem.Links.Add(newLink);
won't work, since it throws the exception you 've provided in case WI 1001 is already a link of WI 1000.
So we need to check if WI 1001 is already in the links of 1000 before adding. This was possible as follows:
WorkItem workItem = workItemStore.GetWorkItem(1000);
LinkCollection links = workItem.Links;
List<int> relatedWorkItemIds = new List<int>();
foreach (var link in links)
{
relatedWorkItemIds.Add(((Microsoft.TeamFoundation.WorkItemTracking.Client.RelatedLink) (link)).RelatedWorkItemId);
}
if(relatedWorkItemIds.Contains(1001))
{
return;
}
else
{
WorkItemLinkTypeEnd linkTypeEnd = workItemStore.WorkItemLinkTypes.LinkTypeEnds["Child"];
RelatedLink newLink = new RelatedLink(linkTypeEnd, 1001);
workItem.Links.Add(newLink);
}
Working towards answering I realized that what you directly ask "WorkItem.Links.Contains() what does it do?" --> I have no answer.
I hope you can make somehow use of what I 've implemented above.