It's all about the answers!

Ask a question

Need assistance with RTC Query Java Code

Brandy Guillory (5311925) | asked Mar 28 '14, 1:24 a.m.
Hopefully Ralph Schoon will see this. I used the code he provided from his blog work items for automation, but receive a few compilation errors and I was wondering if I could get help resolving those.
public class QueryRTCRunSave  {
    public static void main(String args[]) {
           //creates a progress monitor
              IProgressMonitor monitor = new NullProgressMonitor();
          //stores login credentials in strings
            String repositoryURI = "";
            String userId = "myid";
            String password = "****";
            //client initializes itself
            try {
                ITeamRepository repo = logIntoTeamRepository(repositoryURI,
                            userId, password,  monitor);
            } catch (TeamRepositoryException e) {
                     //this will handle any repository exceptions such as login problems
                     //this shuts down the platform
                     } finally {
            private static ITeamRepository logIntoTeamRepository(String repositoryURI,
                    String userId, String password, IProgressMonitor monitor)
                    throws TeamRepositoryException {
                System.out.println("Trying to log into repository: " + repositoryURI);
                ITeamRepository teamRepository = TeamPlatform
                teamRepository.registerLoginHandler(new LoginHandler(userId, password));
                System.out.println("Login succeeded.");
                return teamRepository;
       private static class LoginHandler implements ILoginHandler, ILoginInfo {

            private String fUserId;
            private String fPassword;

            private LoginHandler(String userId, String password) {
                fUserId = userId;
                fPassword = password;

            public String getUserId() {
                return fUserId;

            public String getPassword() {
                return fPassword;

            public ILoginInfo challenge(ITeamRepository repository) {
                return this;

       /* Do all of my work with RTC servers here. */
      //calls the method findpersonalquery
       IQueryDescriptor query = findPersonalQuery(projectArea, "All Work Items", monitor);<-- projectArea cannot be resolved to a variable

      //method that Finds personal queries stored in repository by project area,name
       public static IQueryDescriptor findPersonalQuery(IProjectArea projectArea,
                String queryName, IProgressMonitor monitor)
                throws TeamRepositoryException {
            // Get the required client libraries
            ITeamRepository teamRepository = (ITeamRepository)projectArea.getOrigin();
            IWorkItemClient workItemClient = (IWorkItemClient) teamRepository.getClientLibrary(
            IQueryClient queryClient = workItemClient.getQueryClient();
            // Get the current user.
            IContributor loggedIn = teamRepository.loggedInContributor();
            IQueryDescriptor queryToRun = null;
            // Get all queries of the user in this project area.
            List queries = queryClient.findPersonalQueries(
                projectArea.getProjectArea(), loggedIn,
                IQueryDescriptor.FULL_PROFILE, monitor);
            // Find a query with a matching name
            for (Iterator iterator = queries.iterator(); iterator.hasNext();) {
                IQueryDescriptor iQueryDescriptor = (IQueryDescriptor);
                if (iQueryDescriptor.getName().equals(queryName)) {
                    queryToRun = iQueryDescriptor;
            return queryToRun;

       //to get unresolved query results stored, you pass query as a parameter
       IQueryResult unresolvedResults = queryClient.getQueryResults(queryToRun);<-- queryClient cannot be resolved, queryToRun cannot be resolved to a variable

     //calls the method processUnresolvedResults
       processUnresolvedResults(projectArea, unresolvedResults, loadProfile, monitor);<-- return type for this method is missing
      //method that processes the unresolvedResults and stores in file
       public static void processUnresolvedResults(IProjectArea projectArea , IQueryResult results,
                ItemProfile profile, IProgressMonitor monitor)
                throws TeamRepositoryException {
            // Get the required client libraries
            ITeamRepository teamRepository = (ITeamRepository)projectArea.getOrigin();
            IWorkItemClient workItemClient = (IWorkItemClient)teamRepository.getClientLibrary(IWorkItemClient.class);
            IAuditableCommon auditableCommon = (IAuditableCommon)teamRepository.getClientLibrary(IAuditableCommon.class);
            long processed = 0;
            while (results.hasNext(monitor)) {<--has next method in IQueryResult is not applicable for arguments IProgressMonitor
                IResult result = (IResult);<--next(IProgressMonitor)is undefined for type IQueryResult
                IWorkItem workItem = auditableCommon.resolveAuditable(
                    (IAuditableHandle) result.getItem(), profile, monitor);
                // Do something with the work item here(save to csv or xml)
            System.out.println("Processed results: " + processed);

3 answers

permanent link
sam detweiler (12.5k6195201) | answered Mar 28 '14, 7:36 a.m.
and another answer  built from Ralph's example
a full workitem example here

second post.

watch out for the forum adding extra double quotes down near the bottom and lowercasing the datatype names.

this will print out all the attributes of all the workitems returned in the query. takes 5 parms
repo, userid, password, projectname, queryname

Brandy Guillory commented Mar 28 '14, 3:12 p.m.

Thank you your information was very helpful!!!

permanent link
Karthik Krishnan (8825118163) | answered Mar 28 '14, 3:51 a.m.
You must initialize projectArea

I've attached similar method which find shared query

IProjectArea projectArea = null;
ITeamRepository teamRepository  = null;
String sProjectAreaName="your Project Area";
IProcessClientService processClient = (IProcessClientService) teamRepository.getClientLibrary( IProcessClientService.class);
URI uri = URI.create(sProjectAreaName.replaceAll(" ","%20"));
projectArea = (IProjectArea) processClient.findProcessArea(uri,null,null);
List<IProjectArea> sharingTargets =new ArrayList<IProjectArea>();
IQueryDescriptor sharedQuery = findSharedQuery(projectArea,sharingTargets,sQueryName,monitor);

IQueryDescriptor findSharedQuery(IProjectArea projectArea, List<IProjectArea> sharingTargets, String queryName,IProgressMonitor monitor) throws TeamRepositoryException 
        ITeamRepository teamRepository =(ITeamRepository) projectArea.getOrigin();
        IWorkItemClient workItemClient =(IWorkItemClient) teamRepository.getClientLibrary(IWorkItemClient.class);
        IQueryClient queryClient = workItemClient.getQueryClient();
        IQueryDescriptor queryToRun = null;
        List<IQueryDescriptor> queries = queryClient.findSharedQueries(
                projectArea.getProjectArea(), sharingTargets,
                QueryTypes.WORK_ITEM_QUERY, IQueryDescriptor.FULL_PROFILE,
        for (Iterator<IQueryDescriptor> iterator = queries.iterator();
                iterator.hasNext();) {
            IQueryDescriptor iQueryDescriptor =
            if (iQueryDescriptor.getName().equals(queryName)) {
                queryToRun = iQueryDescriptor;
        if(queryToRun == null) {
            logger.error("Unable to find the shared query - " + queryName);
        return queryToRun;

Brandy Guillory commented Mar 28 '14, 3:13 p.m.

Thank you I was thinking along those lines and what you gave me was exactly what i needed!

permanent link
Ralph Schoon (63.2k33646) | answered Mar 28 '14, 9:48 a.m.
Hi Brandy,

if you have missing code pieces, try searching the blog e.g. for projectArea. A lot of the code uses the same infrastructure. Links to this are also provided: e.g. the wiki entry on Programmatic Work Item Creation or in

Unfortunately I can not package all the code and sometimes the blog software also interferes with the code.

Brandy Guillory commented Mar 28 '14, 3:14 p.m.

Thank you Ralph, I will do the blog search I totally understand about not being able to package it all.

Your answer

Register or 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.