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,

IWorkItem workingWI = (IWorkItem) wiToUpdate.getWorkingCopy();
IStatus saveWorkItemStatus = getWorkItemServer().saveWorkItem2(
workingWI, null, null);"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.4k6160182) | 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.