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:

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