C# TFS 客户端 API - 查询以获取链接到特定文件的工作项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/618203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 10:27:42  来源:igfitidea点击:

TFS Client API - Query to get work items linked to a specific file?

c#.netvisual-studio-2008tfs

提问by amazedsaint

We are writing a custom tool using TFS client APIs, to connect to TFS, to fetch work items for a project etc.

我们正在使用 TFS 客户端 API 编写自定义工具,以连接到 TFS,获取项目的工作项等。



We are querying the work item store, using WIQL.

我们正在使用 WIQL 查询工作项存储。

Given a fully qualified filename, what is the easiest way to get a list of work items that have change sets which contain the specified file?

给定一个完全限定的文件名,获取具有包含指定文件的更改集的工作项列表的最简单方法是什么?

采纳答案by Martin Woodward

I'm not sure that there is an easy way to do the query that you are requesting using the TFS API. I know that you definately cannot do it using WIQL. I think, using the API, you would have to iterate over all the work items - get the changeset links in them and then look in each changeset for the file path that you are after. This is obviously not much use.

我不确定是否有一种简单的方法可以执行您使用 TFS API 请求的查询。我知道您绝对不能使用 WIQL 来做到这一点。我认为,使用 API 时,您必须遍历所有工作项 - 获取其中的变更集链接,然后在每个变更集中查找您要查找的文件路径。这显然没有多大用处。

You could get that data using the TFS Data Warehouse database however. This information will lag behind the live operational store information because the warehouse only gets updated periodically - but will allow you to track things by the folder/file dimension pretty easily.

但是,您可以使用 TFS 数据仓库数据库获取该数据。此信息将滞后于实时运营商店信息,因为仓库只会定期更新 - 但允许您很容易地按文件夹/文件维度跟踪事物。

回答by Ivan Bohannon

This little code snippet will get you a collection of work items given a TFS server name and a project.. It also filters out work items in the deleted state.

这个小代码片段将为您提供给定 TFS 服务器名称和项目的工作项集合。它还过滤掉处于已删除状态的工作项。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

    namespace EngineTFSAutomation
    {
        class TFSHelper
        {
            static public WorkItemCollection QueryWorkItems(string server, string projectname)
            {
                TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer(server);
                WorkItemStore workItemStore = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));
                Project p = workItemStore.Projects[projectname];
                string wiqlQuery = "Select * from Issue where [System.TeamProject] = '"+projectname+"'";
                wiqlQuery += " and [System.State] <> 'Deleted'";
                wiqlQuery+= " order by ID";
                WorkItemCollection witCollection = workItemStore.Query(wiqlQuery);
                return witCollection;
            }
        }
    }

回答by DevT

For TFS 2012

对于TFS 2012

    Uri tfsUri = new Uri("http://xxx.xx.xx.xxx:8080/tfs2012");
    TfsConfigurationServer configurationServer =TfsConfigurationServerFactory.GetConfigurationServer(tfsUri);                  
    ReadOnlyCollection<CatalogNode> collectionNodes = configurationServer.CatalogNode.QueryChildren(new[] {CatalogResourceTypes.ProjectCollection },false,CatalogQueryOptions.None);   

    foreach (CatalogNode collectionNode in collectionNodes)
    {
        // Use the InstanceId property to get the team project collection
        Guid collectionId = new Guid(collectionNode.Resource.Properties["InstanceId"]);
        TfsTeamProjectCollection teamProjectCollection = configurationServer.GetTeamProjectCollection(collectionId);

        WorkItemStore workItemStore = (WorkItemStore)teamProjectCollection.GetService(typeof(WorkItemStore));

        string query = "SELECT [System.Id] FROM WorkItems where [Assigned to]=@Me";
        WorkItemCollection queryResults = workItemStore.Query(query);

    }

回答by ASout

For TFS 2013 following code works to access TFS project Information:

对于 TFS 2013,以下代码可用于访问 TFS 项目信息:

var tfsUri = new Uri("http://tfs.xxx.xxx.com:8080/tfs/myCollection");
var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(tfsUri);
tfs.EnsureAuthenticated();
var iis = tfs.GetService<Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore>();
// here access to a particular Project with its name
var uriWithGuid = iis.Projects["My project name"].Uri;