It's all about the answers!

Ask a question

Adding Attachments to Work Item


alberto rosponi (2122) | asked Nov 03 '11, 7:28 a.m.
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);

5 answers



permanent link
SEC Servizi (97123559) | answered Jan 18 '12, 9:58 a.m.

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.

Comments
SEC Servizi commented Oct 05 '12, 10:03 a.m. | edited Oct 05 '12, 10:05 a.m.
Ok, we have to get the attachment again when we save it:
att = wiServer.saveAttachment(att, monitor);
Then, the attachment ID will be correct.

permanent link
alberto rosponi (2122) | answered Nov 03 '11, 10:04 a.m.
mmm, i can't find createAttachment, saveAttachment or similar in the Extensions Workshop...

permanent link
Ralph Schoon (63.1k33645) | answered Nov 03 '11, 9:12 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
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.

permanent link
alberto rosponi (2122) | answered Nov 03 '11, 9:06 a.m.
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...

permanent link
Ralph Schoon (63.1k33645) | answered Nov 03 '11, 8:59 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
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.

Your answer


Register or to post your answer.