How to Programmatically Modify a TFS Query with C#

I had to change TFS Iterations and didn’t want to break all existing queries. I also didn’t want to update them all manually.

Thanks to Tarun Arora for a good starting point: http://geekswithblogs.net/TarunArora/archive/2011/07/10/tfs-2010-sdk-get-projects-iterations-area-path-queries-and.aspx

Here’s how to update a query in code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://yourtfsurl/"));
var store = tfs.GetService<WorkItemStore>();
var project = store.Projects["your project name"];
foreach (StoredQuery query in project.StoredQueries)
{
if (query.QueryText.Contains("[System.IterationPath] under 'your area path'"))
{
query.QueryText = query.QueryText.Replace("[System.IterationPath] under 'your area path'",
"[System.IterationPath] under 'your iteration path'");
query.Update();
}
}
}
}
}
Share