It's all about the answers!

Ask a question

How to update workitem custom attributes?


Sudeshna Mitra (2332831) | asked Aug 16 '12, 4:07 a.m.
How to update workitem custom attributes values and save workitem on server side using operation participant ?
Any code example ?

One answer



permanent link
Jared Russell (1.3k12019) | answered Aug 16 '12, 4:33 a.m.
edited Aug 16 '12, 4:36 a.m.
 You first need to get a reference to the work item being saved as part of the operation:
Object data = operation.getOperationData();
if(data instanceof ISaveParameter) {
	ISaveParameter saveParameter = (ISaveParameter) data;
	IAuditable newState = saveParameter.getNewState();
	if(newState instanceof IAuditable) {
		IWorkItem workItemNewState = (IWorkItem) newState;
You then need to get a reference to the attribute(s) you want to update:
IWorkItemServer workItemServer = getService(IWorkItemServer.class);
IAttribute customAttribute = 
		workItemServer.findAttribute(workItemNewState.getProjectArea(), "customAttributeId", null);
You then need to get a working copy of the work item you want to update:
IWorkItem workingCopy = (IWorkItem) workItemNewState.getWorkingCopy();
Lastly you need to update the value of the attribute and save the working copy:
workingCopy.setValue(customAttribute, "my new value");
workItemServer.saveWorkItem2(workingCopy, null, null);

Full code looks like this:
public void run(AdvisableOperation operation, IProcessConfigurationElement participantConfig,
		IParticipantInfoCollector collector, IProgressMonitor monitor) throws TeamRepositoryException {
	Object data = operation.getOperationData();
	if(data instanceof ISaveParameter) {
		ISaveParameter saveParameter = (ISaveParameter) data;
		IAuditable newState = saveParameter.getNewState();
		if(newState instanceof IAuditable) {
			IWorkItem workItemNewState = (IWorkItem) newState;
			
			IWorkItem workingCopy = (IWorkItem) workItemNewState.getWorkingCopy();
			IWorkItemServer workItemServer = getService(IWorkItemServer.class);
			IAttribute customAttribute = 
					workItemServer.findAttribute(workItemNewState.getProjectArea(), "customAttribute", null);
			workingCopy.setValue(customAttribute, "my new value");
			workItemServer.saveWorkItem2(workingCopy, null, null);

		}
	}
}
	
	

Comments
Sudeshna Mitra commented Aug 16 '12, 6:15 a.m. | edited Aug 16 '12, 6:22 a.m.

Thanks for reply Jared,

I tried your code but I was getting the following exception:

Exception running followup action.

An unhandled exception occurred during "testing participant".

Stale Data.

any help???


praveen patidar commented Aug 16 '12, 6:47 a.m.

Remove the workItemServer.saveWorkItem2(workingCopy, null, null); line from the code. As the operation advisor is running in the current context so this method will be called automatically. Saving work item twice is causing error.

If I am not wrong this code you are running inside the server then I thin working copy is also not needed.

First remove the line above if not working then do something like this

IWorkItem workItemNewState = (IWorkItem) newState;

        IWorkItemServer workItemServer = getService(IWorkItemServer.class);
        IAttribute customAttribute = 
                workItemServer.findAttribute(workItemNewState.getProjectArea(), "customAttribute", null);
        workItemNewState.setValue(customAttribute, "my new value");

Sudeshna Mitra commented Aug 16 '12, 8:08 a.m.

Hi praveen, as you said , I tried but it was not working , it wont give exception but the workitem was also not save custom attribute value .

any other help please ..........

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.