Getting work Items through Plain Java API
I try to get work Items of a specific project area through the plain Java API.
I managed to get all project areas with the following code:
// repo is a ITeamRepository
IProcessItemService connect = (IProcessItemService) repo
.getClientLibrary(IProcessItemService.class);
List<IProjectArea> p = connect.findAllProjectAreas(null, monitor);
Afterwards I'd like to get all WorkItems of a project area. How can I do this?
I am working with Plain Java Client Libraries Version 3.0.1.2
Best regards
Accepted answer
auditableClient = (IAuditableClient) repository.getClientLibrary(IAuditableClient.class);
IQueryClient queryClient = (IQueryClient) repository.getClientLibrary(IQueryClient.class);
IQueryableAttribute attribute = QueryableAttributes.getFactory(IWorkItem.ITEM_TYPE).findAttribute(currProject, IWorkItem.PROJECT_AREA_PROPERTY, auditableClient, null);
Expression expression = new AttributeExpression(attribute, AttributeOperation.EQUALS, currProject);
IQueryResult<IResolvedResult<IWorkItem>> results = queryClient.getResolvedExpressionResults(currProject, expression, IWorkItem.FULL_PROFILE);
In my code, currProject would be the IProjectArea pointer to the current project as you loop through the List of project areas p in your code.
The IQueryResult object 'results' then contains a list of IResolvedResult records with all of the work items for that project you can iterate through and find properties for each work item.
Hope this helps.
Comments
Also see http://rsjazz.wordpress.com/2012/10/29/using-work-item-queris-for-automation/ and http://rsjazz.wordpress.com/2012/11/19/using-expressions-for-automation/ and http://rsjazz.wordpress.com/2012/11/19/using-an-expression-to-synchronize-attributes-for-work-items-of-a-specific-type/ as examples for this kind of automation.
Hello,
This answer is very perfect if I create a new java client program.
However, how can I obtain the same results (I would to search some workitems) on a server side program ? I cannot use this code using the component ITeamRepository because this method is not available on the server-side.
In fact, when I click on Save, I would like to search some workitems to execute some actions ...
Thanks for your help
Regards
Mathieu
To run a query on the server, you can use IQueryCommon which is a common service - in fact, you can probably use it on the client as well. The rest should look like in
http://rsjazz.wordpress.com/2012/11/19/using-expressions-for-automation/ or
http://rsjazz.wordpress.com/2012/10/29/using-work-item-queris-for-automation/
How to create work items was just recently explained here: https://jazz.net/forum/questions/170304/rtc-api-creating-workitems-and-modifying-them-serverside/170306
1 vote
Thanks Ralph.
I have understood this point : I must use IQueryCommon ...
But I don't understand how I can create or get this object from my "Save" action ?
Moreover, this link is very interesting : https://jazz.net/forum/questions/52742/how-to-query-in-server-plug-in
But, I try to resolve this 2 lines without success :
IWorkItemServer workItemServer = getService(IWorkItemServer.class);
IQueryCommon queryService= getService(IQueryCommon.class);
Regards,
About
workItemServer
, I have found solution.
I must use auditableCommon created with this code :
Object data = operation.getOperationData();
ISaveParameter param = (ISaveParameter) data;
auditableCommon = param.getSaveOperationParameter().getAuditableCommon();
You get it using
fWorkItemCommon = getService(IWorkItemCommon.class);
You can only use getService if your class extends com.ibm.team.repository.service.AbstractService for example
public class MyParticipant extends AbstractService implements IOperationParticipant {
See examples such as https://rsjazz.wordpress.com/2012/07/31/rtc-update-parent-duration-estimation-and-effort-participant/ on that blog.
1 vote
Thanks a lot Ralph !
My code can be compiled !!!!
now I try to add the extension bloc on the plugin configuration but it fails because I use operationsAdvisors instead of operationParticipants (in your example)
I have a warning message on the xml plugin configuration : Elements "prerequisites" is not legal for "extensionsService" ...
Have you already used prerequisites with operationsAdvisors ?
Thanks in advance
Mathieu
Ralph,
I have modified my plugin to operationParticipants instead of operationsAdvisors and it is the same situation ...
Elements "prerequisites" is not legal for "extensionsService" ...
I don't understand why ???
I get the same warning, but it works
The warning is normal and does not create problems. If you leave the prerequisites out, you do. The schema should be adjusted, I think.
I think the Extensions Workshop also mentions the warning is normal. It is not related to participants or advisors. You get it in both.
Please open a new question if you have a question.
There are also advisors as well as participants published on https://rsjazz.wordpress.com/ you will see, they all have the same issue - just a warning only however.
1 vote
5 other answers
thank's for the answer. Now I get all WorkItems. Is it possible to get only the work Items with e.g. type defect?
I tried it with the following code, but still get all Work Items.
IAttributeVariable<String> variable = new WorkItemTypeVariable("defect");
System.out.println(variable.getDisplayName());
IQueryableAttribute attribute = QueryableAttributes.getFactory(IWorkItem.ITEM_TYPE) .findAttribute(currProject, IWorkItem.PROJECT_AREA_PROPERTY, auditableClient, monitor);
Expression expression = new VariableAttributeExpression(attribute,
AttributeOperation.EQUALS, variable);
IQueryResult<IResolvedResult<IWorkItem>> results = queryClient
.getResolvedExpressionResults(currProject, expression,
IWorkItem.FULL_PROFILE);
Is there a way to do this?
Best regards
I am trying to query a list of workitem base certain criteria:
term.add(projectAreaExpression);
term.add(workItemTypeExpression);
term.add(categoryExpression);
The query works fine until I try to add the filter of the workitem state.
The query returns null object. Could someone shed some light how to generate the query to look up list of workitem base on its state?
Thanks.
IQueryableAttribute stateAttribute = findAttribute(teamRepository, projectArea, IWorkItem.STATE_PROPERTY, monitor);
AttributeExpression workItemStateTypeExpression = new AttributeExpression(stateAttribute, AttributeOperation.EQUALS, "Open");
...
term.add(workItemStateTypeExpression);
Code snippet:
IQueryableAttribute projectAreaAttribute= findAttribute(teamRepository, projectArea, IWorkItem.PROJECT_AREA_PROPERTY, monitor);
AttributeExpression projectAreaExpression= new AttributeExpression(projectAreaAttribute, AttributeOperation.EQUALS, projectArea);
IQueryableAttribute categoryAttribute = findAttribute(teamRepository, projectArea, IWorkItem.CATEGORY_PROPERTY, monitor);
AttributeExpression categoryExpression = new AttributeExpression(categoryAttribute, AttributeOperation.EQUALS, category);
IQueryableAttribute workItemTypeAttribute = findAttribute(teamRepository, projectArea, IWorkItem.TYPE_PROPERTY, monitor);
AttributeExpression workItemTypeExpression = new AttributeExpression(workItemTypeAttribute, AttributeOperation.EQUALS, WorkItemType.getIdentifier());
IQueryableAttribute stateAttribute = findAttribute(teamRepository, projectArea, IWorkItem.STATE_PROPERTY, monitor);
AttributeExpression workItemStateTypeExpression = new AttributeExpression(stateAttribute, AttributeOperation.EQUALS, "Open");
/* //Owner is Kelvin Lui"
IQueryableAttribute ownerAttribute = findAttribute(teamRepository, projectArea, IWorkItem.OWNER_PROPERTY, monitor);
AttributeExpression ownerTypeExpression = new AttributeExpression(ownerAttribute, AttributeOperation.EQUALS, "Kelvin Lui" );
System.out.println(" ownerAttribute.getAttributeType()" + ownerAttribute.getAttributeType() + " "+
ownerAttribute.getDisplayName() + " " + ownerAttribute.getIdentifier());*/
/*//WorkItem ID
IQueryableAttribute idAttribute = findAttribute(teamRepository, projectArea, IWorkItem.ID_PROPERTY, monitor);
//AttributeExpression idExpression = new AttributeExpression(idAttribute, AttributeOperation.EQUALS, "57320" );
//new AttributeExpression(idAttribute, AttributeOperation.EQUALS, "57320" );
*/
//System.out.println("ownerTypeExpress: "+ownerTypeExpression.getValue()+ " "+ ownerTypeExpression.getAttributeIdentifier());
//Build the Query by doing an AND Operation among the 3 above sub-queries
Term term= new Term(Operator.AND);
term.add(projectAreaExpression);
term.add(workItemTypeExpression);
term.add(categoryExpression);
term.add(workItemStateTypeExpression);
//term.add(idExpression);
/* IQueryClient queryClient = (IQueryClient) teamRepository.getClientLibrary(IQueryClient.class);
com.ibm.team.workitem.common.query.IQueryResult<IResolvedResult<IWorkItem>> result =
queryClient.getResolvedExpressionResults(projectArea, term, IWorkItem.FULL_PROFILE);
*/
// Evaluate Query
IQueryCommon queryService= getQueryCommon(teamRepository);
//ItemProfile<IWorkItem> profile= this.getProfile(projectAreaAttribute).createExtension(IWorkItem.CREATOR_PROPERTY);
this.getProfile(projectAreaAttribute).createExtension(IWorkItem.CREATOR_PROPERTY);
//IQueryResult<IResolvedResult<IWorkItem>> result= queryService.getResolvedExpressionResults(projectArea, term, profile);
IQueryResult<IResolvedResult<IWorkItem>> result= queryService.getResolvedExpressionResults(projectArea, term, IWorkItem.FULL_PROFILE);
//Add All Results in a list and return that list
List<IWorkItem> matchingWorkItems= new ArrayList<IWorkItem>(result.getResultSize(monitor).getTotalAvailable());
while (result.hasNext(monitor)) {
matchingWorkItems.add(result.next(monitor).getItem());
}
can someone share or attach the whole source-code for getting ALL WorkItems without predefined queries? My Idea behind is to list all project areas and to list all worktimes for every project areas.
The first step is done! I get all my project areas in a customized way...but now i'm trying to get the workitems and that doesn't work with the help of the tutorials.
That's why I need the source to see the imports, main-class and the other methods. Always there is something missing or i am not able to include your codes.
Best regards!
Comments
You can only get work items in a project area.
Start here: https://rsjazz.wordpress.com/2015/09/30/learning-to-fly-getting-started-with-the-rtc-java-apis/
Example code expression to find work items:
https://rsjazz.wordpress.com/2012/11/19/using-an-expression-to-synchronize-attributes-for-work-items-of-a-specific-type/
A complete example to create work items
https://rsjazz.wordpress.com/2015/02/27/a-rtc-workitem-command-line-version-2-2/