如何在C#中从TFS中检索工作项列表?

我正在尝试用WPF / C#编写项目报告工具。 我想访问TFS(Team Foundation Server)上的所有项目名称,然后显示给定项目中每个工作项的统计信息。

我有项目名称,但获得实际的工作项目正在给我带来困难。 这是我到目前为止所得到的:

public const string tfsLocation = "http://whatever"; // get the top list of project names from the team foundation server public List LoadProjectList() { var tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(tfsLocation)); var workItemStore = new WorkItemStore(tpc); var projects = (from Project project in workItemStore.Projects select project.Name).ToList(); return projects; } public string GetProjectInfo(string targetProject) { string info = String.Empty; var tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(tfsLocation)); var workItemStore = new WorkItemStore(tpc); foreach (Project project in workItemStore.Projects) { if (project.Name == targetProject) { info += String.Format("Project: {0}\n\n", project.Name); info += "Work Item Types:\n"; foreach (WorkItemType item in project.WorkItemTypes) { info += String.Format("- {0}\n", item.Name); info += String.Format(" - Description: {0}\n", item.Description); info += " - Field Definitions:\n"; foreach (FieldDefinition field in item.FieldDefinitions) { info += String.Format(" - {0}\n", field.Name); } info += "\n"; } } } return info; } 

GetProjectInfo发回一些关于每个项目中的内容的有用信息,但到目前为止看起来我只看到WorkItems的定义 ,而不是实际的 WorkItems本身。 我认为我编写的编程看错了地方。

从Microsoft对WorkItem的定义( http://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.workitemtracking.client.workitem.aspx )来看,它看起来像是在WorkItemTracking.Client中,但不在WorkItemStore内部,我不知道去哪里访问它。

最终版本:

在参考下面的答案后,这是我的函数的更新版本。 这只是返回一长串工作项名称之间的新行,用于打印输出,这就是我正在努力工作(现在)。

 public string GetProjectInfo(string targetProject) { string info = String.Empty; var tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(tfsLocation)); WorkItemStore workItemStore = new WorkItemStore(tpc); Query query = new Query(workItemStore, "SELECT * FROM WorkItems WHERE [System.TeamProject] = @project", new Dictionary() { { "project", targetProject } }); WorkItemCollection wic = query.RunQuery(); foreach (WorkItem item in wic) { info += String.Format("{0}\n", item.Title); } return info; } 

您需要使用WIQL查询来获取您感兴趣的实际工作项,例如获取特定项目的所有工作项:

 using Microsoft.TeamFoundation.WorkItemTracking.Client; Query query = new Query( workItemStore, "select * from issue where System.TeamProject = @project", new Dictionary() { { "project", project.Name } } ); var workItemCollection = query.RunQuery(); foreach(Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem workItem in workItemCollection) { /*Get work item properties you are interested in*/ foreach(Microsoft.TeamFoundation.WorkItemTracking.Client.Field field in workItem.Fields) { /*Get field value*/ info += String.Format("Field name: {0} Value: {1}\n", field.Name, field.Value); } }