It's all about the answers!

Ask a question

Why does a programatically created custom attribute appear in the Database but nowhere else?


Andrew Soloninka (5511924) | asked Apr 29 '13, 1:15 p.m.
I created a custom attribute with the following code:

if (!teamNameExists(wiToUpdate)) {
IProjectArea projectArea = fetchProjectArea(wiToUpdate);
String identifier = Messages.getString("Attribute.Identifier"); //$NON-NLS-1$
String displayName = Messages.getString("Attribute.Display.Name"); //$NON-NLS-1$
String attributeType = Messages.getString("Attribute.Type"); //$NON-NLS-1$

IAttribute teamAttribute = getWorkItemCommon().createAttribute(
projectArea, identifier, attributeType, displayName,
monitor);

wiToUpdate.addCustomAttribute(teamAttribute);
IWorkItem workingWI = (IWorkItem) wiToUpdate.getWorkingCopy();
IStatus saveWorkItemStatus = getWorkItemServer().saveWorkItem2(
workingWI, null, null);
logger.info("Status of Attribute Save is: "
+ saveWorkItemStatus.toString());
}

When I opened the project that the work item was associated with and selected  the Process tab. I checked the Project Configuration > Configuration Data > Types and Attributes  and selected the correct Type. The custom attribute I had created and saved did not exist. However, when I attempted to manually enter the attribute, I got an error saying the attribute ID already existed. 

When I try to retrieve the custom attribute programatically, created with the code above, the workItem method getCustomAttributes() returns null.

One answer



permanent link
Kevin Ramer (4.5k8183200) | answered Apr 29 '13, 5:03 p.m.
I've found that the project area must know about the attribute before it can "cleanly" be in the work item context.  I use this little ditty:

protected IAttribute getAttribute(IProjectAreaHandle area,
            String attributeId, boolean createIt)
            throws TeamRepositoryException {
        IWorkItemClient service = getWorkItemClient();
        IAttribute attribute = service.findAttribute(area, attributeId, null);
        if (null == attribute && createIt) {
            attribute = service.createAttribute(area, attributeId,
                    AttributeTypes.MEDIUM_STRING, attributeId, null);
        }
        return attribute;
    }

which seems to do the trick.   The above method is followed by a workItem.addCustomAttribute( returned attribute from above)


Your answer


Register or 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.