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

How to add custom attributes to work items programmatically

Hi,

Can I ask how to add a custom attribute to a work item programmatically? Suppose the custom attribute is:

Name: JiraKey
ID: jiraKey
Type: Small String

We cannot add this attribute through Project Configuration > Configuration Data > Work Items > Types and Attributes in advance because we don't know what are the specific custom attributes we are going to add at compile time. Let me use the above attribute as an example, how can I add it to a work item directly? Any documentation or tutorial about this? thank you very much

Lin

0 votes



4 answers

Permanent link
I modified the code to get rid of the above the exception:

IAttribute attribute = workItemClient.createAttribute(projectArea,
"testJiraKey5", "Small String",
"TestJiraKey", null);


IWorkItem workItemFromCopy = workingCopy.getWorkItem();
workItemFromCopy.addCustomAttribute(attribute);
workItemFromCopy.setValue(attribute, "JRM-Test");

But then I got some new exceptions

java.lang.ClassCastException: java.lang.String incompatible with
com.ibm.team.workitem.common.model.Identifier
at

"Small String" is not a valid attribute type and will be interpreted as
a custom enumeration. Setting enumeration values requires Identifiers
(that are read from the enumeration).

As you need a string attribute, change the attribute creation code to read:

IAttribute attribute = workItemClient.createAttribute(projectArea,
"testJiraKey5", AttributeTypes.SMALL_STRING,
"TestJiraKey", monitor);


--
Regards,
Patrick
Jazz Work Item Team

1 vote


Permanent link
here is the code I tried

IAttribute attribute = workItemClient.createAttribute(projectArea, "testJiraKey2", "Small String", "Test Jira Key", null);
System.out.println("attribute id: " + attribute.getIdentifier());


workItem.addCustomAttribute(attribute);
workItem.setValue(attribute, "JRM-Test");



Then I got the exceptions:
attribute id: TestJiraKey2
com.ibm.team.repository.common.internal.ImmutablePropertyException
at com.ibm.team.repository.common.internal.util.ItemUtil$ProtectAdapter.notifyChanged(ItemUtil.java:2026)
at org.eclipse.emf.common.notify.impl.BasicNotifierImpl.eNotify(BasicNotifierImpl.java:247)
at org.eclipse.emf.ecore.util.EcoreEList.dispatchNotification(EcoreEList.java:255)
at org.eclipse.emf.common.notify.impl.NotifyingListImpl.addUnique(NotifyingListImpl.java:310)
at org.eclipse.emf.common.util.BasicEList.add(BasicEList.java:626)
at com.ibm.team.workitem.common.internal.model.impl.WorkItemImpl.addCustomAttribute(WorkItemImpl.java:2949)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:79)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:618)
at com.ibm.team.repository.common.internal.util.ItemStore$ItemInvocationHandler.invoke(ItemStore.java:596)
at $Proxy27.addCustomAttribute(Unknown Source)
at com.ibm.team.workitem.ide.ui.example.CreateWorkItemsInBg$1.runProtected(CreateWorkItemsInBg.java:202)
at com.ibm.team.foundation.client.util.FoundationJob.run(FoundationJob.java:68)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)

0 votes


Permanent link
I modified the code to get rid of the above the exception:

IAttribute attribute = workItemClient.createAttribute(projectArea, "testJiraKey5", "Small String", "TestJiraKey", null);


IWorkItem workItemFromCopy = workingCopy.getWorkItem();
workItemFromCopy.addCustomAttribute(attribute);
workItemFromCopy.setValue(attribute, "JRM-Test");

But then I got some new exceptions

java.lang.ClassCastException: java.lang.String incompatible with com.ibm.team.workitem.common.model.Identifier
at com.ibm.team.workitem.common.internal.model.impl.WorkItemImpl.setValue(WorkItemImpl.java:2872)
at com.ibm.team.workitem.ide.ui.example.CreateWorkItemsInBg$1.runProtected(CreateWorkItemsInBg.java:233)
at com.ibm.team.foundation.client.util.FoundationJob.run(FoundationJob.java:68)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)

Any idea?

0 votes


Permanent link
Yes, that's the problem. Thank you very much for your reply.

Lin

I modified the code to get rid of the above the exception:

IAttribute attribute = workItemClient.createAttribute(projectArea,
"testJiraKey5", "Small String",
"TestJiraKey", null);


IWorkItem workItemFromCopy = workingCopy.getWorkItem();
workItemFromCopy.addCustomAttribute(attribute);
workItemFromCopy.setValue(attribute, "JRM-Test");

But then I got some new exceptions

java.lang.ClassCastException: java.lang.String incompatible with
com.ibm.team.workitem.common.model.Identifier
at

"Small String" is not a valid attribute type and will be interpreted as
a custom enumeration. Setting enumeration values requires Identifiers
(that are read from the enumeration).

As you need a string attribute, change the attribute creation code to read:

IAttribute attribute = workItemClient.createAttribute(projectArea,
"testJiraKey5", AttributeTypes.SMALL_STRING,
"TestJiraKey", monitor);


--
Regards,
Patrick
Jazz Work Item Team

0 votes

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
× 10,939

Question asked: Mar 22 '10, 11:00 a.m.

Question was seen: 9,065 times

Last updated: Mar 22 '10, 11:00 a.m.

Confirmation Cancel Confirm