Programmatically creating a work item in the java client api's.
public static IWorkItemHandle createWorkItem(ITeamRepository repo, IProgressMonitor monitor) throws TeamRepositoryException {
ITeamAreaHandle teamAreaHandle = ... //how do i find the team area by name???
ITeamArea teamArea = (ITeamArea) repo.itemManager().fetchCompleteItem(teamAreaHandle, IItemManager.DEFAULT, monitor);
IProjectAreaHandle projectArea = teamArea.getProjectArea();
IWorkItemClient service = (IWorkItemClient) repo.getClientLibrary(IWorkItemClient.class);
IWorkItemType workItemType = service.findWorkItemType(projectArea, "defect", monitor);
IWorkItemHandle handle = service.getWorkItemWorkingCopyManager().connectNew(workItemType, monitor);
WorkItemWorkingCopy wc = service.getWorkItemWorkingCopyManager().getWorkingCopy(handle);
IWorkItem workItem = wc.getWorkItem();
try {
List<ICategory> findCategories= service.findCategories(projectArea, ICategory.FULL_PROFILE, monitor);
ICategory category = findCategories.get(0);
workItem.setCategory(category);
workItem.setCreator(repo.loggedInContributor());
workItem.setOwner(repo.loggedInContributor());
workItem.setHTMLSummary(XMLString.createFromPlainText("Example work item"));
IDetailedStatus s = wc.save(null);
if(! s.isOK()) {
throw new TeamRepositoryException("Error saving work item", s.getException());
}
} finally {
service.getWorkItemWorkingCopyManager().disconnect(workItem);
}
workItem = (IWorkItem) repo.itemManager().fetchCompleteItem(workItem, IItemManager.DEFAULT, monitor);
monitor.subTask("Created a work item " + workItem.getId());
}
2 answers
If you need more code examples e.g. how to set attribute values, or find team areas by name look here: https://rsjazz.wordpress.com/2015/02/27/a-rtc-workitem-command-line-version-2-2/ and search the blog for keywords.
private void createWorkPackage(String sFId, String sSummary, String sDescript, String sTeamCat, String sPlanDate, String sDueDate, String sOwner) { try { IProgressMonitor monitor = new NullProgressMonitor(); IWorkItemClient workItemService = (IWorkItemClient) targetRepository .getClientLibrary(IWorkItemClient.class); IWorkItemHandle handle = workItemService.getWorkItemWorkingCopyManager() .connectNew(mappedInternalWorkItemType, monitor); WorkItemWorkingCopy wc = workItemService.getWorkItemWorkingCopyManager() .getWorkingCopy(handle); IWorkItem workItem = wc.getWorkItem(); try { ListfindCategories = workItemService.findCategories( targetProjectArea, ICategory.FULL_PROFILE, monitor); Integer nCount = 0; ICategory tmpCategory; do { tmpCategory = findCategories.get(nCount++); } while (nCount =findCategories.size()) throw new TeamRepositoryException("Error applying Category to WI"); workItem.setCategory(tmpCategory); workItem.setCreator(workPackageInitialCreator); workItem.setOwner(workPackageInitialOwner); workItem.setHTMLSummary(XMLString .createFromPlainText(sSummary)); workItem.setHTMLDescription(XMLString .createFromPlainText(sDescript)); try { if (!workItem.hasCustomAttribute(externalIDWorkItemAttribute)) { IAttribute newAttribute = externalIDWorkItemAttribute; workItem.addCustomAttribute(newAttribute); workItem.setValue(newAttribute, sFId); } } catch (RuntimeException e) { System.out.println(e.getMessage()); System.out.println(e.toString()); e.printStackTrace(); } IDetailedStatus s = wc.save(monitor); if (!s.isOK()) { throw new TeamRepositoryException("Error saving work item", s.getException()); } } finally { workItemService.getWorkItemWorkingCopyManager().disconnect(workItem); } workItem = (IWorkItem) targetRepository.itemManager().fetchCompleteItem( workItem, IItemManager.DEFAULT, monitor); monitor.subTask("Created a work item " + workItem.getId()); } catch (TeamRepositoryException e) { // TODO Auto-generated catch block e.printStackTrace(); } }