首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从TFS WorkItemTrackingHttpClient QueryByWiqlAsync中分页结果

如何从TFS WorkItemTrackingHttpClient QueryByWiqlAsync中分页结果
EN

Stack Overflow用户
提问于 2020-01-20 02:51:25
回答 1查看 1.8K关注 0票数 1

我使用WorkItemTrackingHttpClient.QueryByWiqlAsync检索查询的结果。默认情况下,它返回前200条记录。它有一个顶参数,您可以使用它来指定小于200的值。

但是,我的查询结果大于200条记录。有人知道如何使用带有适当分页.i.e的TFS客户端执行查询吗?页码和页码?例如,返回第10页,其中页计数为50。

我已经看过了,但是我看不出如何使用客户机来完成这个任务,这是令人惊讶的,因为它的功能看起来非常基本。

我正在使用Nuget软件包Microsoft.TeamFoundationServer.Client 16.153.0。我还连接到TFS 2017 onprem。

EN

回答 1

Stack Overflow用户

发布于 2020-01-20 10:22:13

是的,我们只能返回一个工作项列表,该列表通过调用API限制为最多200项。它是按设计设计的,详情请参见工作项目-列表

但是,我们可以使用WIQL查询从Azure DevOps检索数据。它非常灵活,可以在任何情况下使用。见WIQL查询用于DevOps查询的Azure WIQL Rest

要获得所有工作项,我们可以尝试以下步骤:

  1. 使用工作项查询API执行存储的查询,以检索工作项ID的列表。
  2. 将工作项ID列表拆分为200组,这是工作项API支持的最大批处理大小。
  3. 为每个包含200个工作项ID的列表调用工作项API,以获取工作项详细信息。

有关详细信息,可以参考这条线

此外,下面的代码供您参考,它正在为我工作:

代码语言:javascript
运行
复制
using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;
using Microsoft.VisualStudio.Services.Client;
using Microsoft.VisualStudio.Services.Common;
using System;
using System.Collections.Generic;
using System.Linq;

namespace QueryWorkitems0619
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri uri = new Uri("http://tfs2017:8080/tfs/DefaultCollection");

            //string PAT = "xxxx";
            string project = "SCRUM-TFVC";

            //VssBasicCredential credentials = new VssBasicCredential(user, PAT);

            VssCredentials credentials = new VssClientCredentials();
            credentials.Storage = new VssClientCredentialStorage();

        //create a wiql object and build our query
        Wiql wiql = new Wiql()
        {
            Query = "Select * " +
                    "From WorkItems " +
                    "Where [Work Item Type] = 'Product Backlog Item' " +
                    "And [System.TeamProject] = '" + project + "' " +
                    "And [System.State] <> 'Closed' " +
                    "Order By [State] Asc, [Changed Date] Desc"
        };

        //create instance of work item tracking http client
        using (WorkItemTrackingHttpClient workItemTrackingHttpClient = new WorkItemTrackingHttpClient(uri, credentials))
        {
            //execute the query to get the list of work items in the results
            WorkItemQueryResult workItemQueryResult = workItemTrackingHttpClient.QueryByWiqlAsync(wiql).Result;

            //Splict the query result (the list of work item IDs) into groups of 200.
            var QueryGroups = from i in Enumerable.Range(0, workItemQueryResult.WorkItems.Count())
                            group workItemQueryResult.WorkItems.ToList()[i] by i / 200;

            foreach (var QueryGroup in QueryGroups)
            {
                //some error handling                
                if (QueryGroup.Count() != 0)
                {
                    //need to get the list of our work item ids and put them into an array
                    List<int> list = new List<int>();
                    foreach (var item in QueryGroup.ToList())
                    {
                        list.Add(item.Id);
                    }
                    int[] arr = list.ToArray();

                    //build a list of the fields we want to see
                    string[] fields = new string[3];
                    fields[0] = "System.Id";
                    fields[1] = "System.Title";
                    fields[2] = "System.State";

                    //get work items for the ids found in query
                    var workItems = workItemTrackingHttpClient.GetWorkItemsAsync(arr, fields, workItemQueryResult.AsOf).Result;

                    Console.WriteLine("\n\n----------------------------------------------------------------");
                    Console.WriteLine("\nQuery Results: {0} items found for Group {1}", workItems.Count, QueryGroup.Key);
                    Console.WriteLine("\n----------------------------------------------------------------");

                    //loop though work items and write to console
                    foreach (var workItem in workItems)
                    {
                        Console.WriteLine("ID:{0} Title:{1}  State:{2}", workItem.Id, workItem.Fields["System.Title"], workItem.Fields["System.State"]);
                    }         
                }                 
            }
            Console.ReadLine();
        }
    }
}

}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59816438

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档