How to update workitem custom attributes?
How to update workitem custom attributes values and save workitem on server side using operation participant ?
Any code example ? |
One answer
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"); 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 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;
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
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.