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

Adding Attachments to Work Item

Hi,
I tried to follow the indications in other topics, but i have still problems in adding attachments to a work item! Anyone could help me?

This is the final part of my code. "note" is a xml String; "wi_ref" are the references of my work item.
After running the code, no errors are found, but nothing appears in the work item "Attachments" table :(



IAttachment att = wiServer.createAttachment(prAreaH, monitor);

IContent cont = contServ.storeText(IContent.CONTENT_TYPE_XML, note, null);
att.setContent(cont);

att = (IAttachment) att.getWorkingCopy();

wiServer.saveAttachment(att, monitor);

IItemReference reference = WorkItemLinkTypes.createAttachmentReference(att);

wi_ref.add(WorkItemEndPoints.ATTACHMENT, reference);

0 votes



5 answers

Permanent link
Hi,

the code should look like this:


private void attachFile(String name, WorkItemWorkingCopy workItem,
IProjectArea fProjectArea, IWorkItemClient workItemClient)
throws TeamRepositoryException, IOException {
File attachmentFile = new File(name);
FileInputStream fis = new FileInputStream(attachmentFile);
try {
IAttachment newAttachment = workItemClient.createAttachment(
fProjectArea, attachmentFile.getName(), "", "text",
"UTF-8", fis, null);

newAttachment = (IAttachment) newAttachment.getWorkingCopy();
newAttachment = workItemClient.saveAttachment(newAttachment, null);
IItemReference reference = WorkItemLinkTypes
.createAttachmentReference(newAttachment);
workItem.getReferences().add(WorkItemEndPoints.ATTACHMENT,
reference);
} finally {
if (fis != null) {
fis.close();
if (attachmentFile != null) {
attachmentFile.delete();
}
}
}
}



Try to browse the SDK for usage. See the Extending Team Concert article on how to set that up.

1 vote


Permanent link
Hi,

the code should look like this:


private void attachFile(String name, WorkItemWorkingCopy workItem,
IProjectArea fProjectArea, IWorkItemClient workItemClient)
throws TeamRepositoryException, IOException {
File attachmentFile = new File(name);
FileInputStream fis = new FileInputStream(attachmentFile);
try {
IAttachment newAttachment = workItemClient.createAttachment(
fProjectArea, attachmentFile.getName(), "", "text",
"UTF-8", fis, null);

newAttachment = (IAttachment) newAttachment.getWorkingCopy();
newAttachment = workItemClient.saveAttachment(newAttachment, null);
IItemReference reference = WorkItemLinkTypes
.createAttachmentReference(newAttachment);
workItem.getReferences().add(WorkItemEndPoints.ATTACHMENT,
reference);
} finally {
if (fis != null) {
fis.close();
if (attachmentFile != null) {
attachmentFile.delete();
}
}
}
}



Try to browse the SDK for usage. See the Extending Team Concert article on how to set that up.




Thanks for your reply!
I have to add the attachment from the server side, so i can't use client.createAttachment, but the corresponding server.createAttachment, which doesn't receive the same parameters..
I didn't find indications for the server side anywhere...

0 votes


Permanent link
Did you search the API in Eclipse? How to set that up for developing server extensions is shown in Extending RTC workshop in the libray. Just mark your method ans search for references.

There is also now an API Javadoc for 3.0.1.1.

I can't debug this right now.

0 votes


Permanent link
mmm, i can't find createAttachment, saveAttachment or similar in the Extensions Workshop...

0 votes


Permanent link

This is the final part of my code. "note" is a xml String; "wi_ref" are the references of my work item.
After running the code, no errors are found, but nothing appears in the work item "Attachments" table :(



IAttachment att = wiServer.createAttachment(prAreaH, monitor);

IContent cont = contServ.storeText(IContent.CONTENT_TYPE_XML, note, null);
att.setContent(cont);

att = (IAttachment) att.getWorkingCopy();

wiServer.saveAttachment(att, monitor);

IItemReference reference = WorkItemLinkTypes.createAttachmentReference(att);

wi_ref.add(WorkItemEndPoints.ATTACHMENT, reference);

Your steps are correct.
1. Create the attachment:
IContent cont = contServ.storeText(IContent.CONTENT_TYPE_XML, note, null);

IAttachment att = wiServer.createAttachment(prAreaH, monitor);
att.setContent(cont);

2. Save the attachment to server:
att = (IAttachment) att.getWorkingCopy();

wiServer.saveAttachment(att, monitor);

3. Create a reference to the attachment:
IItemReference reference = WorkItemLinkTypes.createAttachmentReference(att);

IWorkItemReferences references = data.getNewReferences();
references.add(WorkItemEndPoints.ATTACHMENT, reference);

4. Add the reference to a work-item:
IWorkItem wiFreshCopy = (IWorkItem) wiServer.findWorkItemById(wi.getId(), IWorkItem.FULL_PROFILE, null).getWorkingCopy();

wiServer.saveWorkItem2(wiFreshCopy, references, null);

The trick is to get always a working copy of the items. :wink:

Notice that the attachment's id will be -1... Does anyone know why?
Thanks in advance.

0 votes

Comments
Ok, we have to get the attachment again when we save it:
att = wiServer.saveAttachment(att, monitor);
Then, the attachment ID will be correct.

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: Nov 03 '11, 7:28 a.m.

Question was seen: 7,229 times

Last updated: Oct 05 '12, 10:05 a.m.

Confirmation Cancel Confirm