如何在TFS API中从产品待办事项项中检索子任务列表



给定一个特定的产品待办列表id,我想以编程方式检索作为PBI子任务的任务列表。

我知道任务中没有一个字段说"父PBI Id"。我有一个版本的代码可以工作,但这真的很慢,因为我已经在客户端执行了部分过滤。

查看当前进度:

string wiqlQuery =
    string.Format(
        "Select ID, [Remaining Work], State " +
        "from WorkItems " +
        "where (([Work Item Type] = 'Task')" +
        " AND ([Iteration Path] = '{0}' )" +
        " AND (State <> 'Removed')" +
        " AND (State <> 'Done')) ",
        sprint, storyId);
// execute the query and retrieve a collection of workitems
WorkItemCollection workItems = wiStore.Query(wiqlQuery);
if (workItems.Count <= 0)
    return null;
var result = new List<TaskViewModel>();
foreach (WorkItem workItem in workItems)
{
    var relatedLink = workItem.Links[0] as RelatedLink;
    if (relatedLink == null) continue;
    if (relatedLink.RelatedWorkItemId != storyId) continue;
    Field remWorkField = (from field in workItem.Fields.Cast<Field>()
                          where field.Name == "Remaining Work"
                          select field).FirstOrDefault();
    if (remWorkField == null) continue;
    if (remWorkField.Value == null) continue;
    var task = new TaskViewModel
    {
        Id = workItem.Id,
        ParentPbi = relatedLink.RelatedWorkItemId,
        RemainingWork = (double) remWorkField.Value,
        State = workItem.State
    };
    result.Add(task);
}
return result;

作为标准,MSF敏捷中的团队项目带有一组查询。看一下"工作项"->"迭代1"->"迭代待办事项列表"。

将此查询保存为磁盘中的WIQL文件是完全可能的。
使用它作为修改后的wiqlQuery应该可以减轻您所做的许多过滤。

EDIT(回应评论:"好,我这样做了,但是查询没有提到父项和链接(子)项之间的关系"):

我打开了默认的"迭代Backlog"的WIQL:

<?xml version="1.0" encoding="utf-8"?>
<WorkItemQuery Version="1">
  <TeamFoundationServer>
  http://****>
  <TeamProject>****</TeamProject>
  <Wiql>SELECT [System.Id], [System.WorkItemType], [System.Title],
  [System.State], [System.AssignedTo],
  [Microsoft.VSTS.Scheduling.RemainingWork],
  [Microsoft.VSTS.Scheduling.CompletedWork],
  [Microsoft.VSTS.Scheduling.StoryPoints],
  [Microsoft.VSTS.Common.StackRank],
  [Microsoft.VSTS.Common.Priority],
  [Microsoft.VSTS.Common.Activity], [System.IterationPath],
  [System.AreaPath] FROM WorkItemLinks WHERE
  (Source.[System.TeamProject] = @project and
  Source.[System.AreaPath] under @project and
  Source.[System.IterationPath] under '****Iteration 1' and
  (Source.[System.WorkItemType] = 'User Story' or
  Source.[System.WorkItemType] = 'Task')) and
  [System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward'
  and Target.[System.WorkItemType] = 'Task' ORDER BY
  [Microsoft.VSTS.Common.StackRank],
  [Microsoft.VSTS.Common.Priority] mode(Recursive)</Wiql>
</WorkItemQuery>

查询中检索相关项的部分应该是这样的

[System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward'

最新更新