It's all about the answers!

Ask a question

Work item attachment links


Steve Gates (9134) | asked Feb 08 '12, 3:12 p.m.
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:

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



permanent link
sam detweiler (12.5k6185201) | answered Feb 09 '12, 8:44 a.m.
I don't see how to get Attachments as a result field in the query result..

Sam

permanent link
Steve Gates (9134) | answered Feb 09 '12, 9:37 a.m.
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();
}
}

permanent link
sam detweiler (12.5k6185201) | answered Feb 09 '12, 12:48 p.m.
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

permanent link
Steve Gates (9134) | answered Feb 09 '12, 4:31 p.m.
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.

permanent link
Steve Gates (9134) | answered Feb 09 '12, 4:38 p.m.
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:


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.

permanent link
scott idler (1) | answered Jul 26 '12, 7:49 p.m.
This looks like Java code.  What is the library used for talking to the RTC server, an SDK?

permanent link
sam detweiler (12.5k6185201) | answered Jul 26 '12, 10:14 p.m.
 Yes, The SDK. See the RTC product downloads. 

Note that there are three kits
plain java (client only)
SDK server side
And build system (for running builds)
Sam

permanent link
Bruno Costa (1122) | answered Jan 09 '13, 7:09 p.m.
I tried the code posted in this thread and it is working fine, thanks, but there's a little "bug": The file appears without name at the canvas "Attachments". Is there something I can do to fix it?

Your answer


Register or to post your answer.