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 :(
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
Hi,
the code should look like this:
Try to browse the SDK for usage. See the Extending Team Concert article on how to set that up.
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.
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...
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.