This method I wrote works fine.
Have a look at it; I hope it helps you.
public List<IWorkItem> getLinkedWorkItems (IWorkItem wi, String linkTypeID, String sourceOrTarget)
throws TeamRepositoryException {
List<IWorkItem> linkedWorkItems = new ArrayList<IWorkItem>();
ILinkService linkService = getService(ILinkService.class);
IRepositoryItemService repositoryItemService = getService(IRepositoryItemService.class);
if (!wi.isNewItem()) {
// existing Work Item
ILinkServiceLibrary linkLibrary = (ILinkServiceLibrary) linkService.getServiceLibrary(ILinkServiceLibrary.class);
IItemReference workItemRef = IReferenceFactory.INSTANCE.createReferenceToItem(wi);
ILinkQueryPage linkPage = linkLibrary.findLinks(linkTypeID, workItemRef);
for (ILink link : linkPage.getAllLinksFromHereOn()) {
IWorkItemHandle workItemHandle = null;
// ricavo i work items linkati che sono source o target del link
if (sourceOrTarget.equals("source")) {
workItemHandle = (IWorkItemHandle) link.getSourceRef().resolve();
}
if (sourceOrTarget.equals("target")) {
workItemHandle = (IWorkItemHandle) link.getTargetRef().resolve();
}
IWorkItem linkedWorkItem = (IWorkItem) repositoryItemService.fetchItem(workItemHandle, IRepositoryItemService.COMPLETE);
linkedWorkItems.add(linkedWorkItem);
}
} else {
// new Work Item nuovo (I am creating it)
// I retrieve all new references
// (that, during work item creation, coincide with all references)
if (!wi.getItemId().equals(workItem.getItemId())) {
throw new TeamRepositoryException("Method getLinkedWorkItems (IWorkItem wi, String linkTypeID, String sourceOrTarget) cannot be used");
}
IEndPointDescriptor endPointDescriptor = null;
if (sourceOrTarget.equals("source")) {
endPointDescriptor = ILinkTypeRegistry.INSTANCE.getLinkType(linkTypeID).getSourceEndPointDescriptor();
}
if (sourceOrTarget.equals("target")) {
endPointDescriptor = ILinkTypeRegistry.INSTANCE.getLinkType(linkTypeID).getTargetEndPointDescriptor();
}
IWorkItemReferences refs = saveParameter.getNewReferences();
if (refs.hasReferences(endPointDescriptor)) {
List<IReference> linkedRefList = refs.getReferences(endPointDescriptor);
for (IReference linkedRef : linkedRefList) {
IWorkItemHandle workItemHandle = (IWorkItemHandle) linkedRef.resolve();
IWorkItem linkedWorkItem = (IWorkItem) repositoryItemService.fetchItem(workItemHandle, IRepositoryItemService.COMPLETE);
linkedWorkItems.add(linkedWorkItem);
}
}
}
return linkedWorkItems;
}