2013-02-21 63 views
2

我有使用TFS API檢索工作項目及其所有鏈接工作項目(代碼如下)的工作代碼(感謝John Socha-Leialoha)。但是,我想要做的是訪問鏈接文件的名稱(TFS稱它爲「Versioned Item」),以供每個工作項目使用。在TFS GUI中,您可以將文件鏈接到工作項目。說工作項目1234鏈接到文件foo.txt。現在,當我運行此查詢來查找鏈接項時,該文件不在列表中 - 僅返回其他子WI或父WI。如果我在GUI中完全創建和運行查詢,結果是一樣的。我怎樣才能找出哪些文件鏈接到給定的WI?我現在唯一的方法是在TFS GUI中查看WI,並在右下角的Files列表中顯示。如何從TFS檢索鏈接​​的「版本控制的項目」

也許我只是需要做一個正常的平面查詢,獲取WI領域,不知何故鏈接文件的名稱將是該WI的領域之一?我不需要下載鏈接的文件,我只需要文件名/位置。

代碼返回所有鏈接的是突,在這裏:

public List<string> GetLinkedItems() 
{ 
    //executes a linked item query, returning work items, as well as the items that are link to them. 
    //gets digital asset work item that contains the given part number in the Assoc. Parts field 
    var result = new List<string>(); 
    var tpc = new TfsTeamProjectCollection(new Uri(_tfsUri)); 
    var workItemStore = (WorkItemStore) tpc.GetService(typeof (WorkItemStore)); 
    //and [Schilling.TFS.TechPub.AssocParts] CONTAINS '101-4108' 
    var query = 
     "SELECT [System.Id], [System.Links.LinkType], [System.TeamProject]," + 
     " [System.WorkItemType], [System.Title], [System.AssignedTo]," + 
     " [System.State] FROM WorkItemLinks " + 
     " WHERE ([Source].[System.TeamProject] = 'Tech Pubs' AND " + 
     " [Source].[System.WorkItemType] = 'DigitalAsset' AND " + 
     " [Source].[System.State] <> '') And " + 
     " ([System.Links.LinkType] <> '') And " + 
     " ([Target].[System.WorkItemType] <> '') " + 
     " ORDER BY [System.Id] mode(MayContain)"; 
    var treeQuery = new Query(workItemStore, query); 
    //Note we need to call RunLinkQuery here, not RunQuery, because we are doing a link item type of query 
    var links = treeQuery.RunLinkQuery(); 

    //// Build the list of work items for which we want to retrieve more information// 
    int[] ids = (from WorkItemLinkInfo info in links 
       select info.TargetId).Distinct().ToArray(); 

    // 
    // Next we want to create a new query that will retrieve all the column values from the original query, for 
    // each of the work item IDs returned by the original query. 
    // 
    var detailsWiql = new StringBuilder(); 
    detailsWiql.AppendLine("SELECT"); 
    bool first = true; 

    foreach (FieldDefinition field in treeQuery.DisplayFieldList) 
    { 
     detailsWiql.Append(" "); 
     if (!first) 
      detailsWiql.Append(","); 
     detailsWiql.AppendLine("[" + field.ReferenceName + "]"); 
     first = false; 
    } 
    detailsWiql.AppendLine("FROM WorkItems"); 
    // 
    // Get the work item details 
    // 
    var flatQuery = new Query(workItemStore, detailsWiql.ToString(), ids); 
    WorkItemCollection details = flatQuery.RunQuery(); 

    return 
     (from WorkItem wi in details 
     select wi.Id + ", " + wi.Project.Name + ", " + wi.Title + ", " + wi.State).ToList(); 
} 
+0

從這段代碼你如何知道誰是孩子的父母? – 2018-02-16 06:20:12

回答

2

工作項查詢只能說明WorkItemLinkType鏈接。要獲得其他類型的鏈接(即源代碼管理中的文件),您需要查看WorkItem對象本身中的鏈接列表。下面是一個代碼片段,將讓你連接到工作項文件的神器:

TfsTeamProjectCollection tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(
    new Uri("http://<server>:8080/tfs/<collection>")); 

WorkItemStore wiStore = tpc.GetService<WorkItemStore>(); 

VersionControlServer vc = tpc.GetService<VersionControlServer>(); 

WorkItem task = wiStore.GetWorkItem(<work item with a linked file>); 

var externalLinks = task.Links.OfType<ExternalLink>(); 

foreach (var link in externalLinks) 
{ 
    XmlDocument artifact = vc.ArtifactProvider.GetArtifactDocument(new Uri(link.LinkedArtifactUri)); 
} 

XML文檔包含使用GetItem()方法來抓住從VersionControlServer正確的文件版本所需的所有必要信息。

+0

非常感謝,正是我所需要的! – 2013-02-25 23:15:53