如何从TFS中检索链接的“版本化项目”

时间:2013-02-21 01:33:36

标签: c# api tfs hyperlink

我有工作代码(谢谢John Socha-Leialoha),它使用TFS API检索工作项及其所有链接的工作项(下面的代码)。但是,我要做的是访问每个工作项的链接文件的名称(TFS称之为“Versioned Item”)。在TFS GUI中,您可以将文件链接到工作项。 Say Work Item 1234链接到文件foo.txt。现在,当我运行此查询以查找链接项时,该文件不在列表中 - 仅返回其他子WI或父WI。如果我在GUI中完全创建并运行查询,则结果相同。如何找出链接到给定WI的文件?我现在唯一的办法就是在TFS GUI中查看WI,它显示在右下方的Files列表中。

也许我只需要进行正常的平面查询,获取WI字段,并且链接文件的名称会以某种方式成为该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();
}

1 个答案:

答案 0 :(得分: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文档包含使用VersionControlServer方法从GetItem()获取正确文件版本所需的所有必要信息。