It's all about the answers!

Ask a question

How to programmatically add attachment to workItem?


Anna Katcka (1) | asked Dec 02 '08, 8:58 a.m.
Hi,
I need to programmaticaly add file to WorkItem. I have WorkItem object and File object. What should I do next?
Thank you
Anna

3 answers



permanent link
Marcel Bihr, Jazz Work Item team (1.4k) | answered Dec 03 '08, 3:49 a.m.
JAZZ DEVELOPER
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

permanent link
Kinny Kun (5183) | answered Feb 20 '09, 1:47 p.m.
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!!


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

permanent link
Patrick Streule (4.9k21) | answered Feb 21 '09, 10:58 a.m.
JAZZ DEVELOPER
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

Your answer


Register or to post your answer.