Access TFS Team Query from Client Object API

喜欢而已 提交于 2019-12-04 11:59:56

问题


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

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.

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.

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


回答1:


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


来源:https://stackoverflow.com/questions/26479030/access-tfs-team-query-from-client-object-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!