How to add attachment to workitem by JAVA API?
One answer
See below. getWorkingCopy() returns the workingcopy for the work item that the attachment goes to.
/** * Utility method to upload and attach a file to a work item * * @param fileName * - the file name of the file to upload * @param description * - the description of the attachment * @param contentType * - the content type of the file * @param encoding * - the encoding of the file * * @throws TeamRepositoryException */ private void attachFile(String fileName, String description, String contentType, String encoding) throws TeamRepositoryException { File attachmentFile = new File(fileName); FileInputStream fis; try { fis = new FileInputStream(attachmentFile); try { IAttachment newAttachment = getWorkItemClient() .createAttachment(getWorkItem().getProjectArea(), attachmentFile.getName(), description, contentType, encoding, fis, monitor); newAttachment = (IAttachment) newAttachment.getWorkingCopy(); newAttachment = getWorkItemCommon().saveAttachment( newAttachment, monitor); IItemReference reference = WorkItemLinkTypes .createAttachmentReference(newAttachment); getWorkingCopy().getReferences().add( WorkItemEndPoints.ATTACHMENT, reference); } finally { if (fis != null) { fis.close(); } } } catch (FileNotFoundException e) { throw new WorkItemCommandLineException( "Attach File - File not found: " + fileName, e); } catch (IOException e) { throw new WorkItemCommandLineException( "Attach File - I/O Exception: " + fileName, e); } }