Access TFS Team Query from Client Object API

后端 未结 1 2001
有刺的猬
有刺的猬 2021-02-09 22:40

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

相关标签:
1条回答
  • 2021-02-09 23:24

    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);
    }
    
    0 讨论(0)
提交回复
热议问题