Jazz Forum Welcome to the Jazz Community Forum Connect and collaborate with IBM Engineering experts and users

How to programmatically add attachment to workItem?

Hi,
I need to programmaticaly add file to WorkItem. I have WorkItem object and File object. What should I do next?
Thank you
Anna

0 votes



3 answers

Permanent link
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

0 votes


Permanent link
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

0 votes


Permanent link
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

0 votes

Your answer

Register or log in 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.

Search context
Follow this question

By Email: 

Once you sign in you will be able to subscribe for any updates here.

By RSS:

Answers
Answers and Comments
Question details
× 10,938

Question asked: Dec 02 '08, 8:58 a.m.

Question was seen: 8,972 times

Last updated: Dec 02 '08, 8:58 a.m.

Confirmation Cancel Confirm