2015-10-15 132 views
0

我試圖自動創建和管理TFS項目。我有以下代碼來創建一個TFS項目:創建TFS項目

try 
{ 
    Uri collectionUri = new Uri("http://TFSServer:8080"); 
    TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(collectionUri); 

    WorkItemStore workItemStore = tpc.GetService<WorkItemStore>(); 
    Project teamProject = workItemStore.Projects["ProjectName"]; 
    WorkItemType workItemType = teamProject.WorkItemTypes["ProjectNameTask"]; 

    WorkItem userStory = new WorkItem(workItemType) 
    { 
     Title = "Recently ordered menu 2", 
     Description = "As a return customer, I want to see items that I've recently ordered", 
     State = "Proposed" 

    }; 

    var invalidFields = userStory.Validate(); 

    userStory.Save(); 
} 
catch (Exception exception) 
{ 
    Console.WriteLine("Exception occurred: " + exception.ToString()); 
    throw; 
} 

調用userStory.Validate()在invalidFields將返回一個項目,詳情:

Name: Issue Type 
ReferenceName: IssueType 

但是,工作項目不具備的元素鍵入IssueType。所以下面的代碼給出了編譯錯誤:

WorkItem userStory = new WorkItem(workItemType) 
{ 
    Title = "Recently ordered menu 2", 
    Description = "As a return customer, I want to see items that I've recently ordered", 
    State = "Active", 
    IssueType = "Some Issue" 

}; 

錯誤提示「Can not resolve symbol'IssueType'」。這可能是因爲我沒有訪問特定TeamProject的正確dll。我認爲解決以下兩個想法中的一個可能會有所幫助:

如何爲特定的TeamProject的WorkItem獲取正確的dll?

是否可以將字段添加到對象類型?我試圖通過擴展WorkItem來創建一個新類,但它是名爲Microsoft.TeamFoundation.WorkItemTracking.Client的密封類。

想知道如何解決這個問題。

+0

您可以檢查無效字段的狀態是什麼? - 例如'invalidFields [0] .Status'? – Michael

+0

您應該使用有效的「工作項類型」而不是「ProjectNameTask」 –

+0

@Michael invalidFields [0] .Status顯示InvalidEmpty。 – Romonov

回答

0

工作項目可以具有遠遠超出WorkItem類型中表示的任意字段。查看Fields屬性,查看您所在的字段。

例如,userStory.Fields["IssueType"].Value = "Value"

+0

獲取編譯錯誤: 索引器'Microsoft.TeamFoundation.WorkItemTracking.Client.FieldCollection.this'沒有setter。 – Romonov

+0

我的錯誤 - 我用更正的代碼更新了我的答案。 –

+0

這工作!謝謝! – Romonov