Jazz Forum Welcome to the Jazz Community Forum Connect and collaborate with IBM Engineering experts and users

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.




0 votes



One answer

Permanent link

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;
    }

1 vote

Your answer

Register or log in to post your answer.

Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.

Search context
Follow this question

By Email: 

Once you sign in you will be able to subscribe for any updates here.

By RSS:

Answers
Answers and Comments
Question details
× 6,121

Question asked: Mar 21 '17, 7:54 a.m.

Question was seen: 2,346 times

Last updated: Mar 21 '17, 8:50 a.m.

Confirmation Cancel Confirm