如何通过wiql c#asp.net获取工作项的所有字段详细信息?

时间:2018-02-15 07:48:48

标签: c# asp.net tfs wiql

基本上我的TFS层次结构就像这样

P1
- > task1  
- > task2   
- > task3    
- > task4
p1包含以下字段 故事所有者,故事作者,分配给,州,priorty,标题 enter image description here
和P1 cantains 4任务我想要任务的所有细节与p1字段(故事所有者,故事作者,分配给,州,priorty和标题)。

这个代码我没有得到p1字段的任务详细信息(故事所有者,故事作者,分配给,州,priorty和标题)。

var tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("tfs url"));
                WorkItemStore workItemStore = new WorkItemStore(tpc);

                Query query = new Query(workItemStore, "SELECT * FROM WorkItems WHERE [System.TeamProject] = @project", new Dictionary<string, string>() { { "project", projectname } });

                WorkItemCollection wic = query.RunQuery();
var tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://apactfs.cbre.com:8080/tfs/cbre.apac.applications"));
                WorkItemStore workItemStore = new WorkItemStore(tpc);

                Query query = new Query(workItemStore, "SELECT * FROM WorkItems WHERE [System.TeamProject] = @project", new Dictionary<string, string>() { { "project", projectname } });

                WorkItemCollection wic = query.RunQuery();
foreach (WorkItem item in wic)
                {

                    info += String.Format("{0}\n", item.Title);
                }

2 个答案:

答案 0 :(得分:1)

您可以使用以下查询获取字段: -

WorkItemCollection wic = query.RunQuery();
foreach (WorkItem item in wic)
{
     info += String.Format("{0}\n", item.Title);
     var r = item.Fields.OfType<Microsoft.TeamFoundation.WorkItemTracking.Client.Field>()
                  .Select(x => new
                  {
                      Name = x.Name,
                      Value = x.Value
                      //You can fetch more details here...
                  });
}

请注意,为了演示目的,我在此处定义了变量。您应该创建一个自定义对象并填充它。

此外,如果您遇到与Field,Hierarchy等相关的任何问题,请始终调试代码,它会让您清楚地了解数据的流动方式。在此基础上,您可以创建自定义对象并继续。

答案 1 :(得分:0)

您必须先通过一跳查询获取任务列表,然后获取详细信息。

TfsTeamProjectCollection TTPC = new TfsTeamProjectCollection(new Uri("http://xxx:8080/tfs/xxxCollection/"));
            WorkItemStore wis = TTPC.GetService<WorkItemStore>();
            string project = "projectname";
            string workitemid = "1";
            string wiql = $"SELECT [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State], [System.Tags] FROM WorkItemLinks WHERE (Source.[System.TeamProject] = '{project}' and Source.[System.Id] = {workitemid}) and (Target.[System.TeamProject] = '{project}' and Target.[System.WorkItemType] = 'Task') ORDER BY [System.Id] mode(MustContain)";
            Query query = new Query(wis,wiql);
            WorkItemLinkInfo[] result = query.RunLinkQuery();
            List<WorkItem> tasks = new List<WorkItem> { };
            foreach (WorkItemLinkInfo wili in result)
            {
                if (wili.SourceId == 0)
                {
                    //Get the parent work item here.
                }
                else
                {
                    //Get the details for the linked tasks and add to tasks list.
                    tasks.Add(wis.GetWorkItem(wili.TargetId));
                }
            }