DCSIMG
May 2011 - Posts - Baruch Frei
Sign in | Join | Help

May 2011 - Posts

TFS API–Upload Workitems Query

Published at May 06 2011, 04:55 PM by BaruchF

I recently got a request from one of my customers for code sample

how to programmatically upload Workitem query (wiql) to TFS.

Since the UI doesn’t allow import query from file but only create

new query in editor I decided to share it.

First of all, dealing with queries using API became a little complicated

in TFS 2010 because the folder tree support. I recommend a post

written by my colleague Shai Raiten as very good source to learn it.

(although in his sample code the save method for add/remove

folders is missing Smile).

Here is a code sample how to upload Query to Team Queries folder

   1: //define wiql to upload
   2: string wiql = "SELECT [System.Id], [System.WorkItemType], [System.Title] FROM WorkItems WHERE [System.TeamProject] = @project  ORDER BY [System.Id] ";
   3:  
   4: //create instance of project collection
   5: TfsTeamProjectCollection tfs =
   6:        TfsTeamProjectCollectionFactory.GetTeamProjectCollection(
   7:            new Uri("http://bf-lap:8080/tfs/defaultcollection"));
   8: //get work item store
   9: WorkItemStore wis = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));
  10: //get desired project in work item store by its name
  11: Project teamProject = wis.Projects["VersionControl"];
  12: //get team queries folder
  13: QueryFolder teamQueries = teamProject.QueryHierarchy["Team Queries"] as QueryFolder;
  14: //create query definition in desired folder
  15: QueryDefinition queryDefinition=new QueryDefinition("My New Query",wiql,teamQueries);
  16: //save changes
  17: teamProject.QueryHierarchy.Save();

Result:

image

If we want to upload to any other folder than “Team Queries” or

“My Queries” first we have to find it in the folders tree.

So I wrote a recursive function to find desired folder.

   1: /// <summary>
   2: /// returns QueryFolder in given project by its name
   3: /// </summary>
   4: /// <param name="project"></param>
   5: /// <param name="name"></param>
   6: /// <returns></returns>
   7: public static QueryFolder GetQueryFolder(Project project, string name)
   8: {
   9:     return GetQueryFolder(project.QueryHierarchy as QueryFolder, name);
  10: }
  11: /// <summary>
  12: /// recursive find QueryFolder in Queries tree
  13: /// </summary>
  14: /// <param name="root"></param>
  15: /// <param name="name"></param>
  16: /// <returns></returns>
  17: public static QueryFolder GetQueryFolder(QueryFolder root, string name)
  18: {
  19:     if (root.Name == name)
  20:     {
  21:         return root;
  22:     }
  23:     else
  24:     {
  25:         foreach (QueryItem item in root)
  26:         {
  27:             QueryFolder subFolder = item as QueryFolder;
  28:             if (subFolder != null)
  29:             {
  30:                 QueryFolder folder = GetQueryFolder(subFolder, name);
  31:                 if (folder != null)
  32:                 {
  33:                     return folder;
  34:                 }
  35:             }
  36:         }
  37:     }
  38:     return null;
  39: }

Full code sample can be downloaded from here

Happy Coding !