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
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
4 answers
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
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)
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)
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?
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?
Yes, that's the problem. Thank you very much for your reply.
Lin
"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
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