It's all about the answers!

Ask a question

How to query work items using Approvals with the API?

Anurag Chaudhury (7112) | asked Mar 01 '13, 5:26 p.m.
edited Mar 04 '13, 5:21 a.m. by Ralph Schoon (55.5k23642)
 I am trying to use the API to implement work item retrieval based on Approval and other Quick Information types, such as links. However, I am not sure how to go about it.

I am aware of using IQueryableAttribute ,AttributeExpression for querying using work item attributes. But I do not know how to start with Approvals or other link types such as "Mentions".

Ralph Schoon commented Mar 04 '13, 5:22 a.m.

I kept opening this question so I changed the topic to make sure people understand that this is not about regular queries.

Ralph Schoon commented Mar 04 '13, 5:25 a.m.

I have not tried to create queries that look for approvals. Have you tried to create such a query in the UI, save it and look at how it is composed? Another approach might be to plugin-spy the Eclipse query editor with the SDK installed to find how it handles his case.

Anurag Chaudhury commented Mar 04 '13, 2:46 p.m.

 Hi. I am not sure how to decompose the query, as in see how it is implemented through the API. In the UI, I can select the Approvals and then say select, State and the state is Approved.

This,returns all the Approved WIs.

Also, I tried handling this case like handling other attributes(creating an IQueryable Attribute, inserting that into the AttributeExpression and the term and then running this )

IQueryResult<IResolvedResult<IWorkItem>> result = rtcInternalAttribute.get_queryService().getResolvedExpressionResults
(rtcInternalAttribute.get_projectArea(), term,  IWorkItem.FULL_PROFILE);

However, that gives me an exception saying an instance of IAuditableHandle was expected. 

One answer

permanent link
Anurag Chaudhury (7112) | answered Mar 11 '13, 12:23 p.m.
 So I found a way to do this using the QueryModel API.Basically,I do this:

BaseWorkItemQueryModel workItems = BaseWorkItemQueryModel.WorkItemQueryModel.ROOT;
IStringField state = workItems.internalApprovals().stateIdentifier();
IPredicate pred = state._like("Approved");

IItemQuery query = IItemQuery.FACTORY.newInstance((IItemQueryModel) workItems);

Then you have to get an instance of TeamRepository and use it as follows 

IQueryPage qpage =  rep.getQueryService().queryItems(query, IQueryService.EMPTY_PARAMETERS, IQueryService.ITEM_QUERY_MAX_PAGE_SIZE);

where rep is of type TeamRepository. If this fails,giving you a SIP exception that means you most likely dont have permission to view certain project areas in RTC. You need to get the context ids of the project areas you do have access to and pass it to the queryItemsInContext method (this does the same thing as queryItems but looks only in the project area you specified through the context ids)

After that,you have to loop through each IQueryPage and in each of these extract the IItemHandles as follows - 

List<IItemHandle> itemList = qpage.getItemHandles();

You have to then resolve these IItemHandles to the corresponding IItem .

This API allows you to query anything, from comments to modified by,approvals and any common or custom attribute.

Your answer

Register or to post your answer.