2014-09-18 111 views
8

我正試圖在WPF/C#中編寫項目報告工具。我想訪問我們的TFS(Team Foundation Server)上的所有項目名稱,然後顯示給定項目中每個工作項目的統計信息。如何從C#中的TFS中檢索工作項目列表?

我已經得到了項目名稱,但得到實際的工作項目是什麼讓我很難。下面是我到目前爲止有:

public const string tfsLocation = "http://whatever"; 

// get the top list of project names from the team foundation server 
public List<string> 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本身。我認爲我編寫的編程看錯了地方。

從微軟的 它看起來像它的內部WorkItemTracking.Client,但不是WorkItemStore裏面,我不知道哪裏去訪問它的工作項目的定義, (http://msdn.microsoft.com/en-us/library/microsoft.teamfoundation.workitemtracking.client.workitem.aspx)。

最終版本:

這裏是我的功能的更新版本,它引用了以下的答案後。這只是返回一長串工作項目名稱與之間的新行,用於打印,這正是我想要工作的(現在)。

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<string, string>() { { "project", targetProject } }); 

    WorkItemCollection wic = query.RunQuery(); 

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

    return info; 
} 
+0

我投了票,聽起來很有意思。幫助別人解決這個問題。 – mybirthname 2014-09-18 23:14:09

回答

10

您需要使用WIQL查詢來獲取您感興趣的實際工作項目,例如,以獲得特定項目的所有工作項目:

using Microsoft.TeamFoundation.WorkItemTracking.Client; 

Query query = new Query(
    workItemStore, 
    "select * from issue where System.TeamProject = @project", 
    new Dictionary<string, string>() { { "project", project.Name } } 
); 

var workItemCollection = query.RunQuery(); 
foreach(var workItem in workItemCollection) 
{ 
    /*Get work item properties you are interested in*/ 
    foreach(var field in workItem.Fields) 
    { 
     /*Get field value*/ 
     info += String.Format("Field name: {0} Value: {1}\n", field.name, field.Value); 
    } 
} 
+0

,請記住,針對平面查詢和分層結構有不同的方法 – 2014-09-19 09:53:05

+0

我會檢查一下,看看我能否得到它的工作。謝謝。 – 2014-09-19 16:37:08

+0

您使用了「來自問題」,但是它應該不是「來自WorkItems」嗎?什麼是「問題」的參考? – 2014-09-19 16:48:12

相關問題