When a user opens a bug from a test case, I need to check if the test case is linked to a requirement item and if so I need to link the bug to the requirement item.
How can I know this information?
I use following piece of code:
WorkItemLinkCollection links = _workItem.WorkItemLinks;
foreach (WorkItemLink link in links)
{
}
but I don't know how to get the link type and link id.
WorkItemLink is an abstract base class. ExternalLink, HyperLink, and RelatedLink inherit from it so the link instance will be one of those types. So, check the type of your instance or test it with 'is'. You can also get the RegisteredLink property which gets the friendly name of the link type.
For more info see http://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.workitemtracking.client.link(v=vs.80).aspx
Concerning the id that it's related to, each type is related to something different. For example, the RelatedLink type has a RelatedWorkItemId property which returns the id of the workitem that this workitem is related to. The source id is the id of the workitem that the link collection is on.
http://msdn.microsoft.com/en-US/library/microsoft.teamfoundation.workitemtracking.client.relatedlink_members(v=VS.80).aspx
But, a hyperlink link isn't related to another workitem - it has a location property to get the hyperlink location as a string.
http://msdn.microsoft.com/en-US/library/microsoft.teamfoundation.workitemtracking.client.hyperlink_members(v=VS.80).aspx