How to programmatically add attachment to workItem?
3 answers
Hi Anna
You can have a look at the AddFileAction in the AttachmentsPart.java (com.ibm.team.workitem.ide.ui plugin). Basically:
- Get a working copy for the work item (from WorkItemWorkingCopyManager)
- Then:
- create a reference to the file
- add the reference to the Attachments endpoint
- do not forget to save the work item afterwards (WorkItemWorkingCopyManager.save). The save should also upload the attachment.
Regards
Marcel
Jazz Work Item team
You can have a look at the AddFileAction in the AttachmentsPart.java (com.ibm.team.workitem.ide.ui plugin). Basically:
- Get a working copy for the work item (from WorkItemWorkingCopyManager)
- Then:
IWorkItemReferences references= workingCopy.getReferences();
IReference reference= IReferenceFactory.INSTANCE.createReferenceFromURI(file.toURI(), file.getName());
if (reference != null) {
references.add(WorkItemEndPoints.ATTACHMENT, reference);
}
- create a reference to the file
- add the reference to the Attachments endpoint
- do not forget to save the work item afterwards (WorkItemWorkingCopyManager.save). The save should also upload the attachment.
Regards
Marcel
Jazz Work Item team
Hi Marcel,
I tried to add attachments to the existing work item with the code:
IWorkItemWorkingCopyManager wcm = workItemClient.getWorkItemWorkingCopyManager();
wcm.connect(workItemHandle, ...);
WorkItemWorkingCopy wc = wcm.getWorkingCopy(workItemHandle);
IWorkItemReferences refs = wc.getReferences();
** It failed when get references with exception
"Feature not contained in access profile WorkItem"
Then I tried a different way to get the references with the following code and it ran through ok:
IWorkItemHandle handle = (IWorkItemHandle)wc.getWorkItem().getItemHandle();
IWorkItemReferences refs = workItemClient.resolveWorkItemReferences(handle, monitor);
After I got the references, I do:
IReference ref = IReferenceFactory.INSTANCE.createReferenceFromURI(file.toURI(), file.getAbsolutePath());
if (ref != null) {
refs.add(WorkItemEndPoints.ATTACHMENT, ref);
}
wcm.save(new WorkItemWorkingCopy[]{wc}, monitor);
After running the code, I check the work item I just added the attachment from the web UI (meaning I login to the Jazz Server using https://localhost:9443/jazz, then go to Work Item). The work item is not update and the attachment is not there.
Any idea? What am I missing? Thanks alot for your help.
Thanks!!
I tried to add attachments to the existing work item with the code:
IWorkItemWorkingCopyManager wcm = workItemClient.getWorkItemWorkingCopyManager();
wcm.connect(workItemHandle, ...);
WorkItemWorkingCopy wc = wcm.getWorkingCopy(workItemHandle);
IWorkItemReferences refs = wc.getReferences();
** It failed when get references with exception
"Feature not contained in access profile WorkItem"
Then I tried a different way to get the references with the following code and it ran through ok:
IWorkItemHandle handle = (IWorkItemHandle)wc.getWorkItem().getItemHandle();
IWorkItemReferences refs = workItemClient.resolveWorkItemReferences(handle, monitor);
After I got the references, I do:
IReference ref = IReferenceFactory.INSTANCE.createReferenceFromURI(file.toURI(), file.getAbsolutePath());
if (ref != null) {
refs.add(WorkItemEndPoints.ATTACHMENT, ref);
}
wcm.save(new WorkItemWorkingCopy[]{wc}, monitor);
After running the code, I check the work item I just added the attachment from the web UI (meaning I login to the Jazz Server using https://localhost:9443/jazz, then go to Work Item). The work item is not update and the attachment is not there.
Any idea? What am I missing? Thanks alot for your help.
Thanks!!
Hi Anna
You can have a look at the AddFileAction in the AttachmentsPart.java (com.ibm.team.workitem.ide.ui plugin). Basically:
- Get a working copy for the work item (from WorkItemWorkingCopyManager)
- Then:IWorkItemReferences references= workingCopy.getReferences();
IReference reference= IReferenceFactory.INSTANCE.createReferenceFromURI(file.toURI(), file.getName());
if (reference != null) {
references.add(WorkItemEndPoints.ATTACHMENT, reference);
}
- create a reference to the file
- add the reference to the Attachments endpoint
- do not forget to save the work item afterwards (WorkItemWorkingCopyManager.save). The save should also upload the attachment.
Regards
Marcel
Jazz Work Item team
I tried to add attachments to the existing work item with the code:
IWorkItemWorkingCopyManager wcm =
workItemClient.getWorkItemWorkingCopyManager();
wcm.connect(workItemHandle, ...);
WorkItemWorkingCopy wc = wcm.getWorkingCopy(workItemHandle);
IWorkItemReferences refs = wc.getReferences();
** It failed when get references with exception
"Feature not contained in access profile WorkItem"
The item profile you used is too small and accessing properties that were
not specified in the profile will lead to this exception.
Instead of
ItemProfile<IWorkItem> profile = ItemProfile.computeProfile(workitem);
use
ItemProfile<IWorkItem> profile = IWorkItem.FULL_PROFILE;
--
Regards,
Patrick
Jazz Work Item Team