Search code examples
javatfstfs-sdk

TFS JAVA SDK - How to run shared query


I have a application which useds TFS JAVA SDK 14.0.3 . I have a shared query  on my tfs , how can i run the shared query and get the response back using TFS SDK 14.0.3

Also I could see that the query url will expire in every 90 days , so any better way to execute the shared query?

Now I  have a method to run a query , i want method  to  run shared query also.

public void getWorkItem(TFSTeamProjectCollection tpc, Project project){
    WorkItemClient workItemClient = project.getWorkItemClient();
    // Define the WIQL query.
    String wiqlQuery = "Select ID, Title,Assigned from WorkItems where (State = 'Active') order by Title";
    // Run the query and get the results.
    WorkItemCollection workItems = workItemClient.query(wiqlQuery);
    System.out.println("Found " + workItems.size() + " work items.");
    System.out.println();
    // Write out the heading.
    System.out.println("ID\tTitle");
    // Output the first 20 results of the query, allowing the TFS SDK to
    // page
    // in data as required
    final int maxToPrint = 5;
    for (int i = 0; i < workItems.size(); i++) {
        if (i >= maxToPrint) {
            System.out.println("[...]");
            break;
        }
        WorkItem workItem = workItems.getWorkItem(i);
        System.out.println(workItem.getID() + "\t" + workItem.getTitle());
    }
}

Solution

  • Shared query is a query which has been run and saved, so what you need should be getting a a shared query, not run a shared query. You could refer to case Access TFS Team Query from Client Object API:

    ///Handles nested query folders    
    private static Guid FindQuery(QueryFolder folder, string queryName)
    {
        foreach (var item in folder)
        {
            if (item.Name.Equals(queryName, StringComparison.InvariantCultureIgnoreCase))
            {
                return item.Id;
            }
    
            var itemFolder = item as QueryFolder;
            if (itemFolder != null)
            {
                var result = FindQuery(itemFolder, queryName);
                if (!result.Equals(Guid.Empty))
                {
                    return result;
                }
            }
        }
        return Guid.Empty;
    }
    
    static void Main(string[] args)
    {
        var collectionUri = new Uri("http://TFS/tfs/DefaultCollection");
        var server = new TfsTeamProjectCollection(collectionUri);
        var workItemStore = server.GetService<WorkItemStore>();
    
        var teamProject = workItemStore.Projects["TeamProjectName"];
    
        var x = teamProject.QueryHierarchy;
        var queryId = FindQuery(x, "QueryNameHere");
    
        var queryDefinition = workItemStore.GetQueryDefinition(queryId);
        var variables = new Dictionary<string, string>() {{"project", "TeamProjectName"}};
    
        var result = workItemStore.Query(queryDefinition.QueryText,variables);
    }
    

    By the way, you could also check the REST API in the following link:

    https://learn.microsoft.com/en-us/rest/api/vsts/wit/queries/get