2

我有工作代码(感谢 John Socha-Leialoha),它使用 TFS API 来检索工作项,以及它们所有链接的工作项(下面的代码)。但是,我要做的是访问每个工作项的链接文件的名称(TFS 将其称为“版本化项”)。在 TFS GUI 中,您可以将文件链接到工作项。假设工作项 1234 链接到文件 foo.txt。现在,当我运行此查询以查找链接项目时,该文件不在列表中 - 仅返回其他子 WI 或父 WI。如果我完全在 GUI 中创建和运行查询,结果是一样的。如何找出哪些文件链接到给定的 WI?我现在唯一能做的就是在 TFS GUI 中查看 WI,它会显示在右下角的文件列表中。

也许我只需要做一个普通的平面查询,获取 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();
}
4

1 回答 1

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()方法获取正确文件版本所需的所有必要信息。

于 2013-02-21T19:30:23.367 回答