TFS SDK 2010 – Part 6 – Replace text in all Work Item Query Definitions

by Ewald Hofman 9. March 2010 04:20

Updates

  • 19-3-2010: Added the possibility to change the queries in a specific folder

When you work iterative, you might just want to replace your current release or iteration in the query definitions, instead of creating a bunch of new queries. This post shows you how you can perform a quick and simple replace mechanism to achieve this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.TestManagement.Client;
using System.Diagnostics;

namespace TFS_SDK
{
    class Program
    {
        static void Main(string[] args)
        {
            // The url to the tfs server
            Uri tfsUri = new Uri("http://localhost:8080/tfs/");
            const string projectName = "Agile";
            const string startFolder = null; //@"Team Queries\Release 1\Sprint 2";
            const string searchFor = "Iteration 1";
            const string replaceWith = "Iteration 2";

            // Load the tfs instance
            TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(tfsUri, new UICredentialsProvider());

            // Log in to TFS
            tpc.EnsureAuthenticated();

            var wiStore = tpc.GetService<WorkItemStore>();
            var project = wiStore.Projects[projectName];

            foreach (QueryDefinition queryDefinition in GetAllTeamQueries(project, startFolder))
            {
                if (queryDefinition.QueryText.Contains(searchFor))
                {
                    queryDefinition.QueryText = queryDefinition.QueryText.Replace(searchFor, replaceWith);
                }
            }

            project.QueryHierarchy.Save();
        }

        private static List<QueryDefinition> GetAllTeamQueries(Project project, string startFolder)
        {
            var ret = new List<QueryDefinition>();

            var startQueryFolder = GetQueryFolder(project.QueryHierarchy,
                                                  startFolder == null ? (new string[] { }) : startFolder.Split('\\'));

            ret.AddRange(GetAllTeamQueries(startQueryFolder));

            return ret;
        }

        private static List<QueryDefinition> GetAllTeamQueries(QueryFolder queryFolder)
        {
            var ret = new List<QueryDefinition>();

            foreach (QueryItem queryItem in queryFolder)
            {
                if (queryItem is QueryFolder)
                {
                    ret.AddRange(GetAllTeamQueries(queryItem as QueryFolder));
                }
                else
                {
                    ret.Add(queryItem as QueryDefinition);
                }
            }

            return ret;
        }

        private static QueryFolder GetQueryFolder(QueryFolder queryFolder, string[] folders)
        {
            return folders.Length == 0 ? queryFolder : GetQueryFolder((QueryFolder)queryFolder[folders[0]], folders.Skip(1).ToArray());
        }
    }
}

Tags:

VSTS 2010 | TFS SDK

Comments

7/21/2012 11:48:16 AM #

pingback

Pingback from 1771.in

tfs2010 – How to update current sprint team queries in TFS 2010? | mywebsite

1771.in |

Comments are closed

Powered by BlogEngine.NET 1.6.1.0
Theme by Mads Kristensen


ClusterMap

Statistics

Statistics created at 09 Sep 2009

121 posts
493 comments
328 raters
1947739 visit (1042 per day)
21 users online

Recent comments

Comment RSS