如何通过Java API获取RTC工作项属性数据及关联工作项信息?
Hey there! Let's work through your two main issues—getting the "Filed Against" attribute data and fetching parent/child work items with their properties—with clear fixes and code examples.
Your current code tries to fetch the attribute definition first, but there's a simpler way to get the actual value directly from the work item. Also, let's verify you're using the correct property constant.
First, instead of fetching the attribute definition unless you need metadata like display name, you can get the "Filed Against" value straight from the work item using IWorkItem.CATEGORY_PROPERTY:
// After fetching your workItem successfully Object filedAgainstValue = workItem.getValue(IWorkItem.CATEGORY_PROPERTY); if (filedAgainstValue instanceof ICategory) { ICategory filedAgainst = (ICategory) filedAgainstValue; // Access details like name or ID System.out.println("Filed Against Name: " + filedAgainst.getName()); System.out.println("Filed Against ID: " + filedAgainst.getIdentifier()); }
If you still need the attribute definition for validation or metadata, make sure you're using the right constant and that your project area handle is valid. Here's a corrected version of your attribute fetch code:
IAttribute categoryAttribute = workItemClient.findAttribute( projectAreaHandle, IWorkItem.CATEGORY_PROPERTY, monitor ); if (categoryAttribute != null) { // Use the attribute definition, e.g., get its display name System.out.println("Attribute Display Name: " + categoryAttribute.getDisplayName()); } else { System.err.println("Failed to find Category attribute—check project area handle or property constant."); }
Common pitfalls to check:
- Ensure
projectAreaHandleis a valid, loaded handle for your target project area. - Confirm you have the necessary permissions to access the project area and work item attributes.
- Make sure
monitorisn't canceled or invalid (passnullif you don't need progress tracking, though it's better to use a valid monitor).
To get parent and child work items like Epic → Story relationships, you'll need to work with work item links. Here's how to do it step by step:
Fetching the Parent Work Item
The parent link is typically of type IWorkItemLinkConstants.PARENT_WORK_ITEM_LINK_TYPE_ID. Use this to retrieve the parent:
// Get all links for the work item List<IWorkItemLink> links = workItemClient.findWorkItemLinks( Collections.singletonList(workItem), IWorkItemLinkConstants.PARENT_WORK_ITEM_LINK_TYPE_ID, IWorkItem.FULL_PROFILE, monitor ); for (IWorkItemLink link : links) { IWorkItem parentWorkItem = link.getTargetWorkItem(); if (parentWorkItem != null) { // Get parent properties String parentState = (String) parentWorkItem.getValue(IWorkItem.STATE_PROPERTY); System.out.println("Parent Work Item ID: " + parentWorkItem.getId()); System.out.println("Parent State: " + parentState); // Add other properties like planned time as needed } }
Fetching Child Work Items
For children, use the reverse link type IWorkItemLinkConstants.CHILD_WORK_ITEM_LINK_TYPE_ID:
List<IWorkItemLink> childLinks = workItemClient.findWorkItemLinks( Collections.singletonList(workItem), IWorkItemLinkConstants.CHILD_WORK_ITEM_LINK_TYPE_ID, IWorkItem.FULL_PROFILE, monitor ); for (IWorkItemLink childLink : childLinks) { IWorkItem childWorkItem = childLink.getTargetWorkItem(); if (childWorkItem != null) { // Get child properties String childState = (String) childWorkItem.getValue(IWorkItem.STATE_PROPERTY); // Planned time might be stored in a custom attribute or the built-in one—adjust the property ID Object plannedTime = childWorkItem.getValue("com.ibm.team.workitem.plannedTime"); // Example ID, confirm yours System.out.println("Child Work Item ID: " + childWorkItem.getId()); System.out.println("Child State: " + childState); System.out.println("Child Planned Time: " + plannedTime); } }
Notes for Property Retrieval
- For built-in properties like state, use constants from
IWorkItem(e.g.,IWorkItem.STATE_PROPERTY). - For custom properties like specific planned time fields, you'll need to use their unique attribute ID. You can find this by inspecting the work item type in your RTC web UI or using the attribute definition fetch code from the first section.
- Always cast the returned value from
getValue()to the correct type (e.g.,Stringfor state,Doublefor planned time, etc.).
内容的提问来源于stack exchange,提问作者Rinkal Garg

