Work item attachment links
Hi,
I've written code to add an attachment to a work item using only client side code. As written the, code correctly attaches a file to a work item and it's visible in the GUI when viewing the Links tab in the work item. The issue that I'm having is that if I query the work item and have the Attachments field visible in the query results, the attachment does not show up in the work item list view.
Here's the code I'm using to create the link once I save the attachment to the server:
It seems that I'm missing additional back end links, but I'm not sure if I am or if there's something else going on here.
Any thoughts would be appreciated. Thanks in advance!
Steve
I've written code to add an attachment to a work item using only client side code. As written the, code correctly attaches a file to a work item and it's visible in the GUI when viewing the Links tab in the work item. The issue that I'm having is that if I query the work item and have the Attachments field visible in the query results, the attachment does not show up in the work item list view.
Here's the code I'm using to create the link once I save the attachment to the server:
private static void linkAttachment(IAttachment newAttachment,
IWorkItem workItem, ITeamRepository repository,
IProgressMonitor monitor) {
try{
ILinkManager linkManager = (ILinkManager) repository.getClientLibrary(ILinkManager.class);
IItemReference attachRef = IReferenceFactory.INSTANCE.createReferenceToItem(newAttachment.getItemHandle());
IItemReference wiRef = IReferenceFactory.INSTANCE.createReferenceToItem(workItem.getItemHandle());
ILink link = ILinkFactory.INSTANCE.createLink(WorkItemLinkTypes.ATTACHMENT, attachRef, wiRef);
linkManager.saveLink(link, monitor);
}catch(Exception e){
System.out.println("ERROR - " + e.getMessage());
e.printStackTrace();
}
}
It seems that I'm missing additional back end links, but I'm not sure if I am or if there's something else going on here.
Any thoughts would be appreciated. Thanks in advance!
Steve
8 answers
I found out the error in the code that was causing the missing attachment in the query. When creating the link in the first code I pasted above (linkAttachment method), I had reversed the parent-child relationship in the following line of code:
Parent item (the work item) is first, child item (the attachment) is second. The links are created correctly and the attachment now shows in the work item query listing correctly.
ILink link = ILinkFactory.INSTANCE.createLink(WorkItemLinkTypes.ATTACHMENT, wiRef, attachRef);
Parent item (the work item) is first, child item (the attachment) is second. The links are created correctly and the attachment now shows in the work item query listing correctly.
this is the sentence I am trying to figure out
The issue that I'm having is that if I query the work item and have the Attachments field visible in the query results, the attachment does not show up in the work item list view.
is this a standard query, or some custom code u wrote..
I don't know what 'workitem list view', means..
If I use the standard approach for building queries, which produces a list,
I don't find an option to add the attachments as part of that list.
Sam
I misunderstood your question. When you build the work item query in the UI, under the results layout tab, you can add the Attachments column and see what attachments are associated with a work item.
this is the sentence I am trying to figure out
is this a standard query, or some custom code u wrote..
I don't know what 'workitem list view', means..
If I use the standard approach for building queries, which produces a list,
I don't find an option to add the attachments as part of that list.
Sam
The issue that I'm having is that if I query the work item and have the Attachments field visible in the query results, the attachment does not show up in the work item list view.
is this a standard query, or some custom code u wrote..
I don't know what 'workitem list view', means..
If I use the standard approach for building queries, which produces a list,
I don't find an option to add the attachments as part of that list.
Sam
I don't see how to get Attachments as a result field in the query result..
Sam
Hi Sam,
It took a bit of digging before I got to the attachment too. To get to the attachment, you first have to get a working copy of the work item, then you can find any references to the attachment endpoints. Once you have the references to the attachment, you can generate an attachment handle, and then get at the attachment itself. See my code below. You'll notice that I write some file information out to a simple text file (the writer.append lines) so that I can keep track of the files to push them back into work items at a later time.
IResolvedResult<IWorkItem> resolved = results.next(null);
IWorkItemWorkingCopyManager workingCopyManager = workItemClient.getWorkItemWorkingCopyManager();
IWorkItemHandle foundWorkItem = workItemClient.findWorkItemById(resolved.getItem().getId(), IWorkItem.FULL_PROFILE, null);
workingCopyManager.connect(foundWorkItem, IWorkItem.FULL_PROFILE, null);
WorkItemWorkingCopy workingCopy = workingCopyManager.getWorkingCopy(foundWorkItem);
IWorkItemReferences references = workingCopy.getReferences();
List<IReference> attachments = references.getReferences(WorkItemEndPoints.ATTACHMENT);
if(attachments.size() > 0){
atchCounter++;
for (IReference reference : attachments) {
IItemReference attachmentReference = (IItemReference) reference;
IAttachmentHandle attachmentHandle = (IAttachmentHandle) attachmentReference.resolve();
IAttachment srcAttachment = (IAttachment) repository.itemManager().fetchCompleteItem(attachmentHandle,
IItemManager.DEFAULT, monitor);
IContent srcContent = srcAttachment.getContent();
String srcName = srcAttachment.getName();
writer.append(currProject.getName() + ",");
writer.append(Integer.toString(resolved.getItem().getId()) + ",");
writer.append(srcName + ",");
writer.append(srcContent.getContentType() + ",");
writer.append(srcContent.getCharacterEncoding());
writer.append(newLine);
FileOutputStream fos = null;
try{
fos = new FileOutputStream("C:\\RQM Attachments\\" + srcName);
}catch(FileNotFoundException fnfe){
fnfe.printStackTrace();
System.exit(1);
}
IContentManager cm= repository.contentManager();
cm.retrieveContent(srcAttachment.getContent(), fos, null);
fos.close();
}
}