TFS JAVA SDK - 如何运行共享查询

时间:2018-04-23 10:03:01

标签: java tfs tfs-sdk

我有一个使用TFS JAVA SDK 14.0.3的应用程序。 我在我的tfs上有一个共享查询,如何运行共享查询并使用TFS SDK 14.0.3获取响应

此外,我可以看到查询网址每90天都会过期,那么执行共享查询的更好方法是什么?

现在我有一个运行查询的方法,我也想让方法运行共享查询。

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

1 个答案:

答案 0 :(得分:0)

共享查询是一个已运行并保存的查询,因此您需要获取共享查询,而不是运行共享查询。您可以参考案例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);
}

顺便说一下,您还可以在以下链接中查看REST API:

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