How to get the linked Workitem Ids for any workitem using Server side API.
Hello All,
We need to get the linked workitems for a particular workitem. Currently we are using getNewReferences() API to get the list of linked workitems.
Following is the code snippet for getting the linked workitems references.
IWorkItemReferences ref = saveParameter.getNewReferences();
List<IReference> listReferences = ref.getReferences(linkDescriptor);
How to get the workitem IDs from this references. What are the next set of lines that we need to add for this API.
Request you to assist me on this.
One answer
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;
}