how to get Hiearchical structure in WIQL workitem

纵然是瞬间 提交于 2019-12-24 06:25:16

问题


i have a hiearchial strurcture in TFS like this where a 1 Feature can have 'N' number of Product backlog items and single product Product backlogitem can have 'N' number of task/bugs tree structure

Feature1->
PB1->
Task1,task2,task3

my Query

string querystring = string.Format("select [System.Id], [System.Title],[Story.Author],[Story.Owner],[System.AssignedTo]," +
                " [System.WorkItemType],[Microsoft.VSTS.Scheduling.StoryPoints],[Microsoft.VSTS.Common.Priority]," +
                "[Microsoft.VSTS.Scheduling.Effort], [Actual.Effort.Completed]" +
                ",[System.State]," +
                "[System.IterationPath]" +
                " FROM WorkItemLinks" +
                " WHERE" +
                " ([Source].[System.TeamProject]='{0}'" +
                " and [Source].[System.IterationPath] UNDER 'MRI_SCRUM_GIT\\Pluse Pheonix\\Sprint 1'" +
                " and [Source].[System.WorkitemType]<>'' " +
                ")" +

                " and ([System.Links.LinkType]='System.LinkTypes.Hierarchy-Forward')" +
                " and ([Target].[System.WorkItemType] <> '' )" +
                " ORDER BY [System.Id] " +
               " mode (Recursive)", projectname);

Now how do i get all the fields in C# code like the below image


回答1:


Here is already a code snippet on the following website:

https://docs.microsoft.com/en-us/vsts/integrate/quickstarts/work-item-quickstart

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi.Patch.Json;
using Microsoft.VisualStudio.Services.WebApi.Patch;
using Microsoft.VisualStudio.Services.WebApi;
using System.Net.Http.Headers;
using System.Net.Http;
using Newtonsoft.Json;

public class ExecuteQuery
{
    readonly string _uri;
    readonly string _personalAccessToken;
    readonly string _project;

    /// <summary>
    /// Constructor. Manually set values to match your account.
    /// </summary>
    public ExecuteQuery()
    {
        _uri = "https://accountname.visualstudio.com";
        _personalAccessToken = "personal access token";
        _project = "project name";
    }

    /// <summary>
    /// Execute a WIQL query to return a list of bugs using the .NET client library
    /// </summary>
    /// <returns>List of Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models.WorkItem</returns>
    public List<WorkItem> RunGetBugsQueryUsingClientLib()
    {
        Uri uri = new Uri(_uri);
        string personalAccessToken = _personalAccessToken;
        string project = _project;

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

        //create a wiql object and build our query
        Wiql wiql = new Wiql()
        {
            Query = "Select [State], [Title] " +
                    "From WorkItems " +
                    "Where [Work Item Type] = 'Bug' " +
                    "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;

            //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.State";

                //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("{0}          {1}                     {2}", workItem.Id, workItem.Fields["System.Title"], workItem.Fields["System.State"]);
                }

                return workItems;
            }

            return null;
        }
    }
}


来源:https://stackoverflow.com/questions/48823612/how-to-get-hiearchical-structure-in-wiql-workitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!