从客户端对象 API 访问 TFS 团队查询 [英] Access TFS Team Query from Client Object API

查看:23
本文介绍了从客户端对象 API 访问 TFS 团队查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过 TFS 2013 客户端对象 API 按名称运行共享团队查询

Is there a way to run a shared team query, by name, through the TFS 2013 client object API

我正在编写一个 C# 脚本,该脚本将根据共享团队查询的结果执行一些工作.我不想在 TFS UI 和我的脚本中维护查询;我宁愿只运行我的团队使用的已注册查询,然后只使用结果.当我编写注册查询"时,我只是指我在 TFS UI 中编写并保存为共享查询的查询.

I'm working on a C# script that will do some work based off of the results of a shared team query. I don't want to have to maintain the query in the TFS UI as well as in my script; I'd prefer to just run the registered query that my team uses, but then just play with the results. When I write "registered query" I'm just referring to a query that I wrote in the TFS UI and saved as a shared query.

换句话说:我想使用 TFS UI 创建查询,将文件保存在我的共享查询"列表中,将其命名为foo",然后在我的脚本中从客户端对象 API 访问 foo.

In other words: I'd like to use the TFS UI to create a query, save the file in my "shared queries" list, call it "foo", then access foo from the client object API in my script.

我看到 WorkItemStore 有一个 GetQueryDefinition(GUID) 方法,但是我从哪里获得共享团队的 GUID查询?

I see that there is a GetQueryDefinition(GUID) method off of WorkItemStore, but where would I get the GUID for a shared team query?

推荐答案

应满足您需要的示例代码

Sample code that should do what you need

///Handles nested query folders    
private static Guid FindQuery(QueryFolder folder, string queryName)
{
    foreach (var item in folder)
    {
        if (item.Name.Equals(queryName, StringComparison.InvariantCultureIgnoreCase))
        {
            return item.Id;
        }

        var itemFolder = item as QueryFolder;
        if (itemFolder != null)
        {
            var result = FindQuery(itemFolder, queryName);
            if (!result.Equals(Guid.Empty))
            {
                return result;
            }
        }
    }
    return Guid.Empty;
}

static void Main(string[] args)
{
    var collectionUri = new Uri("http://TFS/tfs/DefaultCollection");
    var server = new TfsTeamProjectCollection(collectionUri);
    var workItemStore = server.GetService<WorkItemStore>();

    var teamProject = workItemStore.Projects["TeamProjectName"];

    var x = teamProject.QueryHierarchy;
    var queryId = FindQuery(x, "QueryNameHere");

    var queryDefinition = workItemStore.GetQueryDefinition(queryId);
    var variables = new Dictionary<string, string>() {{"project", "TeamProjectName"}};

    var result = workItemStore.Query(queryDefinition.QueryText,variables);
}

这篇关于从客户端对象 API 访问 TFS 团队查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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