TFS API Part 30 – Working With Queries

17/11/2010

TFS API Part 30 – Working With Queries

TFS 2010 comes with new abilities for Queries that allows you to define Hierarchy and Direct Links (Read More – Work Item Relations (Visual)).

This is the first post on that subject and this will show how to interact with TFS 2010 Queries – Filter, View and Create New using TFS 2010 API.

image

Download Demo Project

Step 1: Create Project and Add Reference

Create an WPF/WinForm application and add the following references:

  • using Microsoft.TeamFoundation.Client;
  • using Microsoft.TeamFoundation.Server;
  • using Microsoft.TeamFoundation.WorkItemTracking.Client;

All files located under – c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\

Step 2: Connect to Team Foundation Server

(TFS API Part 20: Bye TeamFoundationServer and Welcome TfsTeamProjectCollection)

TeamProjectPicker pp = new TeamProjectPicker(TeamProjectPickerMode.SingleProject,false);
pp.ShowDialog();

if (pp.SelectedProjects.Length > 0)
{
    Tfs = pp.SelectedTeamProjectCollection;
    ProjInfo = pp.SelectedProjects[0];
    Store = (WorkItemStore)Tfs.GetService(typeof(WorkItemStore));

    BuildQueryHierarchy(Store.Projects[ProjInfo.Name].QueryHierarchy);
}

Step 2: Define Queries Hierarchy in TreeView

Each project (TFS 2010) contains an object called QueryHierarchy contains the entire Hierarchy for that project.

/// <summary>

/// Getting Project Query Hierarchy and define the the folders under it.

/// </summary>

/// <param name="QueryHierarchy"></param>

void BuildQueryHierarchy(QueryHierarchy QueryHierarchy)

{

    TreeViewItem root = new TreeViewItem();

    root.Header = ProjInfo.Name;

            
    foreach (QueryFolder query in QueryHierarchy)

    {

        DefineFolder(query, root);

    }

    Queries.Items.Add(root);

}

 

/// <summary>
///
Define Query Folders under TreeView
/// 
</summary>
///
 <param name="query">Query Folder
</param>
///
 <param name="father"></param>
void DefineFolder(QueryFolder query, TreeViewItem father)
{
    TreeViewItem item = new TreeViewItem();
    QueryTypes type = QueryTypes.Folder;

    if (query.IsPersonal)  type = QueryTypes.MyQ;
    else if (query.Name == "Team Queries") type = QueryTypes.TeamQ;

    item.Header = CreateTreeItem(query.Name, type);

    father.Items.Add(item);

    foreach (QueryItem sub_query in query)
    {
        if (sub_query.GetType() == typeof(QueryFolder))
            DefineFolder((QueryFolder)sub_query, item);
        else
            DefineQuery((QueryDefinition)sub_query, item);
    }
}

/// 
<summary>
///
Add Query Definition under a specific Query Folder.
/// 
</summary>
///
 <param name="query">Query Definition - Contains the Query Details
</param>
///
 <param name="QueryFolder">Parent Folder</param>
void DefineQuery(QueryDefinition query, TreeViewItem QueryFolder)
{
    TreeViewItem item = new TreeViewItem();
    QueryTypes type;

    switch (query.QueryType)
    {
        case QueryType.List: type = QueryTypes.FView; break;
        case QueryType.OneHop: type = QueryTypes.DView; break;
        case QueryType.Tree: type = QueryTypes.HView; break;
        default: type = QueryTypes.None; break;
    }

    item.Header = CreateTreeItem(query.Name, type);
    item.Tag = query.QueryText;
    item.MouseDoubleClick += new MouseButtonEventHandler(item_MouseDoubleClick);
    QueryFolder.Items.Add(item);
}
Download Demo Project
Add comment
facebook linkedin twitter email

Leave a Reply