It's all about the answers!

Ask a question

How to retrieve a list of attachments of workitem


Marcel Porz (632) | asked Jun 02 '09, 6:45 a.m.
Hi!

How can I retrieve a list of attachments of a workitem programmatically.

I know how to add an attachment but I don't know how to retrieve the existing attachments of a existing workitem.

Thanks!
Marcel.

3 answers



permanent link
SEC Servizi (97123559) | answered Apr 24 '12, 4:31 a.m.
2. How can I save the attachments to disk?

Once you have an "IAttachment" instance, you can use a "FileOutputStream" to save the attachment to your disk:
IAttachment attachment = ...

String pathname = ...
OutputStream outputStream = new FileOutputStream(pathname);
repo.contentManager().retrieveContent(attachment.getContent(), outputStream, null);

To retrieve the attachments of a work-item you need a "WorkItemWorkingCopy" instance from the "IWorkItem":
ITeamRepository repo = ...

IWorkItem wi = ...
IWorkItemClient wiClient = (IWorkItemClient) repo.getClientLibrary(IWorkItemClient.class);
IWorkItemWorkingCopyManager wcm = wiClient.getWorkItemWorkingCopyManager();
wcm.connect(wi, IWorkItem.FULL_PROFILE, null);
WorkItemWorkingCopy wiWorkingCopy = wcm.getWorkingCopy(wi);

IWorkItemReferences wiReferences = wiWorkingCopy.getReferences();
List<IReference> refList = wiReferences.getReferences(WorkItemEndPoints.ATTACHMENT);
for (IReference ref : refList) {
IItemReference itemRef = (IItemReference) ref;
IAttachmentHandle attH = (IAttachmentHandle) itemRef.resolve();
IAttachment att = (IAttachment) repo.itemManager().fetchCompleteItem(attH, IItemManager.DEFAULT, null);
// ...
}

Cheers.

permanent link
Marcel Porz (632) | answered Jun 02 '09, 8:00 a.m.
How can I retrieve a list of attachments of a workitem
programmatically.

I know how to add an attachment but I don't know how to retrieve the
existing attachments of a existing workitem.

You would do it using the same IWorkItemReferences class used when adding
attachments:

IWorkItemReferences references= workingCopy.getReferences();
List<IReference> attachments=
references.getReferences(WorkItemEndPoints.ATTACHMENT);
for (IReference reference : attachments) {
IItemReference attachmentReference= (IItemReference) reference;
IAttachmentHandle attachmentHandle= (IAttachmentHandle)
attachmentReference.resolve();
}

--
Regards,
Patrick
Jazz Work Item Team

Hi Patrick!

First of all thanks for your fast help.

Perhaps I should post my current code:


// Query the Jazz repository for list of workitems
auditableClient = (IAuditableClient) teamRepository
.getClientLibrary(IAuditableClient.class);
IQueryClient queryClient = (IQueryClient) teamRepository
.getClientLibrary(IQueryClient.class);

IQueryableAttribute attribute = QueryableAttributes.getFactory(
IWorkItem.ITEM_TYPE).findAttribute(projectArea,
IWorkItem.PROJECT_AREA_PROPERTY, auditableClient, null);
Expression expression = new AttributeExpression(attribute,
AttributeOperation.EQUALS, projectArea);
IQueryResult<IResolvedResult<IWorkItem>> results = queryClient
.getResolvedExpressionResults(projectArea, expression,
IWorkItem.FULL_PROFILE);

while (results.hasNext(null)) {

IResolvedResult<IWorkItem> resolved = results.next(null);

...

// Getting existing attachments...

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);


for (IReference reference : attachments) {

IItemReference attachmentReference = (IItemReference) reference;
IAttachmentHandle attachmentHandle = (IAttachmentHandle) attachmentReference
.resolve();

}
}


But now I have some additional questions:
1. Why do I have to use a working copy of the workitem although I already have a reference to the workitem in my iteration?
2. How can I save the attachments to disk?

Do you have any idea? Thanks in advance!
Marcel.

permanent link
Patrick Streule (4.9k21) | answered Jun 02 '09, 7:02 a.m.
JAZZ DEVELOPER
How can I retrieve a list of attachments of a workitem
programmatically.

I know how to add an attachment but I don't know how to retrieve the
existing attachments of a existing workitem.

You would do it using the same IWorkItemReferences class used when adding
attachments:

IWorkItemReferences references= workingCopy.getReferences();
List<IReference> attachments=
references.getReferences(WorkItemEndPoints.ATTACHMENT);
for (IReference reference : attachments) {
IItemReference attachmentReference= (IItemReference) reference;
IAttachmentHandle attachmentHandle= (IAttachmentHandle)
attachmentReference.resolve();
}

--
Regards,
Patrick
Jazz Work Item Team

Your answer


Register or 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.