2016-08-19 48 views
0

我們有一個自定義的TFS工作流,我希望能夠從TFS訪問我可以關閉一個Bug的原因(將活動狀態更改爲關閉狀態),以便我們不必更新每次我們想要調整我們的過程時都要編寫代碼。列出工作項狀態編程方式的原因

這是我到目前爲止有:

WorkItemType wiType = this.GetWorkItemStore().Projects[this.ProjectName].WorkItemTypes["Bug"]; 
    var reason = wiType.FieldDefinitions["Reason"]; 
    var state = wiType.FieldDefinitions["State"]; 

    var filterList = new FieldFilterList();  
    FieldFilter filter = new FieldFilter(wiType.FieldDefinitions[CoreField.State], "Active"); 
    filterList.Add(filter); 

    var allowedReasons = reason.FilteredAllowedValues(filterList); 

但是我沒有得到任何結果。我想列出所有可以關閉錯誤的原因(不可重現,固定等)

回答

1

沒有任何簡單的方法可以通過API直接進行轉換,因爲我知道自從API讀取數據庫允許的值直接。

另一種方法是通過WorkItemType.Export()方法導出workitemtype定義,然後從中獲取信息。 Vaccano在this question中的回答提供了您可以使用的完整代碼示例。

編輯給我如何解決這個使用上述建議的例子:

public static List<Transition> GetTransistions(this WorkItemType workItemType) 
{ 
    List<Transition> currentTransistions; 

    // See if this WorkItemType has already had it's transistions figured out. 
    _allTransistions.TryGetValue(workItemType, out currentTransistions); 
    if (currentTransistions != null) 
    return currentTransistions; 

    // Get this worktype type as xml 
    XmlDocument workItemTypeXml = workItemType.Export(false); 

    // Create a dictionary to allow us to look up the "to" state using a "from" state. 
    var newTransistions = new List<Transition>(); 

    // get the transistions node. 
    XmlNodeList transitionsList = workItemTypeXml.GetElementsByTagName("TRANSITIONS"); 

    // As there is only one transistions item we can just get the first 
    XmlNode transitions = transitionsList[0]; 

    // Iterate all the transitions 
    foreach (XmlNode transition in transitions) 
    { 
    XmlElement defaultReasonNode = transition["REASONS"]["DEFAULTREASON"]; 
    var defaultReason = defaultReasonNode.Attributes["value"].Value; 

    var otherReasons = new List<string>(); 
    XmlNodeList otherReasonsNodes = transition["REASONS"].SelectNodes("REASON"); 
    foreach (XmlNode reasonNode in otherReasonsNodes) 
    { 
     var reason = reasonNode.Attributes["value"].Value; 
     otherReasons.Add(reason); 
    } 

    // save off the transistion 
    newTransistions.Add(new Transition 
    { 
     From = transition.Attributes["from"].Value, 
     To = transition.Attributes["to"].Value, 
     DefaultReason = defaultReason, 
     OtherReasons = otherReasons 
    }); 

    } 

    // Save off this transition so we don't do it again if it is needed. 
    _allTransistions.Add(workItemType, newTransistions); 

    return newTransistions; 
} 
+0

你的回答使我的解決方案,因此,而不是創造我自己的,我在改編代碼複製從您的鏈接的問題 - 請隨時編輯! – Liath