如何获取链接的“版本化项目"?来自TFS [英] How to retrieve linked "versioned item" from TFS

查看:76
本文介绍了如何获取链接的“版本化项目"?来自TFS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have working code (thank you John Socha-Leialoha) that uses the TFS API to retrieve work items, along with all their linked work items (code below). However, what I'm trying to do is access the names of the linked Files (TFS calls it a "Versioned Item") for each work item. In the TFS GUI you can link a file to a work item. Say Work Item 1234 is linked to file foo.txt. Now when I run this query to find linked items, that file is not in the list - only other children WIs or parent WIs are returned. It's the same result if I create and run the query entirely in the GUI. How can I find out which files are linked to a given WI? The only way I can now is to look at the WI in the TFS GUI, and it shows in the Files list in the lower right.

也许我只需要做一个普通的平面查询,获取WI字段,以某种方式链接文件的名称就是该WI的字段之一?我不需要下载链接的文件,只需要文件名/位置.

Perhaps I just need to do a normal flat query, fetch the WI fields, and somehow the names of the linked files would be one of the fields of that WI? I don't need to download the linked file, I just need the filename/location.

返回所有链接的WI的代码在这里:

Code to return all linked WIs is here:

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();
}

推荐答案

工作项查询只能显示WorkItemLinkType链接.要获取其他类型的链接(即源代码管理中的文件),您需要浏览WorkItem对象本身中的链接列表.这是一段代码,它将为您提供链接到工作项的文件的工件:

Work item queries can only show WorkItemLinkType links. To get links of other types (i.e. files in source control), you need to go through the list of links in the WorkItem object itself. Here's a snippet of code that will get you the artifact of the file linked to the work item:

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

The XML document contains all necessary information needed to grab the correct file version from the VersionControlServer using the GetItem() method.

这篇关于如何获取链接的“版本化项目"?来自TFS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆