It's all about the answers!

Ask a question

Programmatically creating a work item in the java client api's.


1
1
Jeff Sloyer (3146) | asked Mar 19 '15, 9:24 a.m.
 I have searched through all the docs and have not been able to find a clear and concise example of how to connect to a project and team area and create a work item.

I have found some code that will create a work item but there is not any examples on how to find a team area by name.

This code is on the client side not server side.

Here is my code.

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



permanent link
Ralph Schoon (60.9k33643) | answered Mar 20 '15, 3:53 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
edited Mar 20 '15, 9:04 a.m.
This is explained here: https://jazz.net/wiki/bin/view/Main/ProgrammaticWorkItemCreation

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.

Comments
Ralph Schoon commented Mar 20 '15, 3:54 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

As a hint, you can use the WorkingCopyManager, but is is better to use the abstract class

WorkItemOperation 

instead.


permanent link
Ralph Schoon (60.9k33643) | answered Mar 20 '15, 5:58 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
If you really want to use the WorkingCopyManager, this code has worked for me:

	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 {
				List findCategories = 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();
		}
	}

Your answer


Register or to post your answer.