我有工作代码(谢谢 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();
}
最佳答案
工作项查询只能显示 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
获取正确文件版本所需的所有必要信息。
关于c# - 如何从 TFS 检索链接的 "versioned item",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14993147/