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

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

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

}

1

1 vote



2 answers

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

1 vote

Comments

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

WorkItemOperation 

instead.


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

1 vote

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

Question asked: Mar 19 '15, 9:24 a.m.

Question was seen: 5,207 times

Last updated: Mar 20 '15, 9:04 a.m.

Confirmation Cancel Confirm