C# C VSTS工作项相关计数字段显示为零,即使我看到VSTS web应用程序中的相关链接

C# C VSTS工作项相关计数字段显示为零,即使我看到VSTS web应用程序中的相关链接,c#,visual-studio,azure-devops,C#,Visual Studio,Azure Devops,我正试图从我们的VSTS托管环境中提取一些信息,我需要将其作为报告呈现。我观察到,API库报告没有与该工作项相关的项目,尽管我看到了VSTS web应用程序中的相关链接 这是我的密码- void Main() { string url = "https://[redacted].visualstudio.com"; string token = "[redacted]"; string project = "[redacted]"; string version

我正试图从我们的VSTS托管环境中提取一些信息,我需要将其作为报告呈现。我观察到,API库报告没有与该工作项相关的项目,尽管我看到了VSTS web应用程序中的相关链接

这是我的密码-

void Main()
{
    string url = "https://[redacted].visualstudio.com";
    string token = "[redacted]";
    string project = "[redacted]";
    string version = "[redacted]";

    VssConnection conn = GetConnection(url, token);
    WorkItemTrackingHttpClient witClient = conn.GetClient<WorkItemTrackingHttpClient>();

    Wiql q = new Wiql();
    q.Query = $"SELECT * FROM WorkItems WHERE [System.TeamProject] = '{project}' AND [System.Tags] CONTAINS '{version}' AND [System.WorkItemType] IN ('Product Backlog Item', 'Defect') ORDER BY [System.CreatedDate] desc";

    var qi = witClient.QueryByWiqlAsync(q).Result;
    var ids = qi.WorkItems.Select(x => x.Id);
    var workitems = witClient.GetWorkItemsAsync(ids).Result.Select(r =>
    {
        return new
        {
            ItemId = r.Id,
            ItemAssignedTo = r.Fields["System.AssignedTo"],
            ItemCreatedBy = r.Fields["System.CreatedBy"],
            ItemTitle = r.Fields["System.Title"],
            ItemType = r.Fields["System.WorkItemType"],
            State = r.Fields["System.State"],
            ItemHasDescription = r.Fields.ContainsKey("System.Description") ? "Yes" : "No",
            ItemHasAcceptanceCriteria = r.Fields.ContainsKey("Microsoft.VSTS.Common.AcceptanceCriteria") ? "Yes" : "No",
            RelatedItems = r.Fields.ContainsKey("System.RelatedLinkCount") ? r.Fields["System.RelatedLinkCount"] : null //This line reports no related links,
            Links = r.Links != null ? r.Links.Links : null //So does this line report null
        };
    });
    workitems.Dump();
    conn.Disconnect();
}

private static VssConnection GetConnection(string accountUri, string personalAccessToken)
{
    var cred = new VssBasicCredential(string.Empty, personalAccessToken);
    VssHttpMessageHandler vssHandler = new VssHttpMessageHandler(cred, VssClientHttpRequestSettings.Default.Clone());
    return new VssConnection(
                        new Uri(accountUri),
                        vssHandler,
                        new DelegatingHandler[] { new SuppressHandler() });
}

public class SuppressHandler : DelegatingHandler
{

}

有没有办法删除这些控制台日志记录?

无法根据您的代码找出问题所在

但是,您可以使用下面的代码示例从VSTS检索工作项信息,它在我这边起作用:

要避免获取这些控制台日志,可以禁用程序输出:

在“输出”窗口中单击鼠标右键->取消选择“程序输出”选项,然后重试

using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;
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("https://{account}.visualstudio.com");
            string PAT = "TokenHere";
            string project = "ProjectName";

            VssBasicCredential credentials = new VssBasicCredential("", PAT);

            //create a wiql object and build our query
            Wiql wiql = new Wiql()
            {
                Query = "Select * " +
                        "From WorkItems " +
                        "Where [Work Item Type] IN ('Product Backlog Item', 'Task') " +
                        "And [System.TeamProject] = '" + project + "' " +
                        "And [System.State] <> 'Closed' " +
                        "And [System.RelatedLinkCount] > '0'" +
                        "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;

                //some error handling                
                if (workItemQueryResult.WorkItems.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 workItemQueryResult.WorkItems)
                    {
                        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.RelatedLinkCount";

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

                    Console.WriteLine("Query Results: {0} items found", workItems.Count);

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

                    Console.ReadLine();
                }
            }
        }
    }
}

我刚刚遇到了一个类似的问题,您需要做的是使用GetWorkItemAsync方法的WorkItemExpand参数,如下所示:

var item = client.GetWorkItemAsync(c_projectName, id, null, null, WorkItemExpand.Relations).Result;

如果不使用Relations属性,则该属性为null,这会产生很大的误导。如果使用,它将存储有关相关工作的正确信息。

Ok我缺少IEnumerable fields参数。一旦我通过了,我就会看到相关链接计数。但是WorkItem.Links属性仍然为空。如何获取相关项目的实际Id?我在LINQPad中尝试了这一点,因此输出在那里并不真正适用。还有其他方法吗?@SohamDasgupta实际Id包含在关系->url中,它不是工作项字段。所以我认为您不能直接获取ID,您可以在循环中检索相关的工作项,然后获取它们的ID。在LINQPad上没有任何经验,不知道如何删除控制台日志记录…这是我之前尝试告诉您的,Relations属性显示为null,否则我现在已经提取了工作项:@SohamDasgupta是的,你可以在另一个线程中引用我的答案,用RESTAPI检索链接的工作项ID:谢谢,但是你知道为什么API不能按预期工作吗?我的意思是,使用API的全部目的是确保我不必乱搞RESTURL。
var item = client.GetWorkItemAsync(c_projectName, id, null, null, WorkItemExpand.Relations).Result;