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

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:

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

0 votes



8 answers

Permanent link
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?

0 votes


Permanent link
 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

0 votes


Permanent link
This looks like Java code.  What is the library used for talking to the RTC server, an SDK?

0 votes


Permanent link
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.

0 votes


Permanent link
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.

0 votes


Permanent link
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

0 votes


Permanent link
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();
}
}

0 votes


Permanent link
I don't see how to get Attachments as a result field in the query result..

Sam

0 votes

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,926

Question asked: Feb 08 '12, 3:12 p.m.

Question was seen: 7,096 times

Last updated: Jan 09 '13, 7:09 p.m.

Confirmation Cancel Confirm