i have a Hierarchical structure like this in which a Feature can have many User Stories each having one to many Tasks.
But a User Story may have no parent Feature as well as some Task may have no parent User Story
F1
-->U1,U2,U3
--->t1,t2,t3
I need a Wiql Query in c# by which for any Taskid input I get its parent storyid (or 0 if it has no parent) along with this latter parent Feature id (0 if it has no parent User Story) and name ('other' if Feature id is 0)
You may find any parent branch for work item with this query:
You can use this code to get a list of parents:
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System;
using System.Collections.Generic;
using System.Linq;
namespace QueryLinkedWIQL
{
class Program
{
static void Main(string[] args)
{
int _id = 742;
try
{
TfsTeamProjectCollection _tpc = new TfsTeamProjectCollection(new Uri("http://myserver/DefaultCollection"));
WorkItemStore _wistore = _tpc.GetService<WorkItemStore>();
var _parents = GetParentsWithWIQL(_wistore, _id);
if (_parents.Count == 0)
{
Console.WriteLine("There is no parent for ID: " + _id);
return;
}
for (int i = 0; i < _parents.Count; i++)
Console.WriteLine("{0} parent: Team project '{1}', Type '{2}', Id '{3}', Title '{4}'",
(i == 0) ? "Main" : "Next", _parents[i].Project.Name, _parents[i].Type.Name, _parents[i].Id, _parents[i].Title);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
Console.WriteLine(ex.StackTrace);
}
}
private static List<WorkItem> GetParentsWithWIQL(WorkItemStore pWiStore, int pId)
{
List<WorkItem> _parents = new List<WorkItem>();
string _wiql = String.Format("SELECT [System.Id] FROM WorkItemLinks WHERE ([Source].[System.WorkItemType] <> '') And ([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward') And ([Target].[System.Id] = {0}) ORDER BY [System.Id] mode(Recursive,ReturnMatchingChildren)", pId);
Query _query = new Query(pWiStore, _wiql);
WorkItemLinkInfo[] _links = _query.RunLinkQuery();
for (int i = 0; i < _links.Count(); i++)
if (_links[i].TargetId != pId) _parents.Add(pWiStore.GetWorkItem(_links[i].TargetId));
return _parents;
}
}
}
Also you can modify the last cycle and get parent info (user story or feature)
if (_parents.Count == 0)
{
Console.WriteLine("There is no parent for ID: " + _id);
return;
}
int _featureId = 0, _userstoryId = 0;
string _featureTitle = "", _userstoryTitle = "";
for (int i = 0; i < _parents.Count; i++)
switch (_parents[i].Type.Name)
{
case "Feature":
if (_featureId == 0) { _featureId = _parents[i].Id; _featureTitle = _parents[i].Title; }
break;
case "Product Backlog Item": //for scrum process template. for agile use User Story
if (_userstoryId == 0) { _userstoryId = _parents[i].Id; _userstoryTitle = _parents[i].Title; }
break;
}
if (_featureId != 0) Console.WriteLine("The main parent is Feature: {0}: '{1}'", _featureId, _featureTitle);
else if (_userstoryId != 0) Console.WriteLine("The main parent is User Story: {0}: '{1}'", _userstoryId, _userstoryTitle);
来源:https://stackoverflow.com/questions/49022991/how-to-get-a-task-id-storyid-featureid-feature-name-by-wiql-in-c-sharp-by-1-qu