TFS API Part 31 – Working With Queries – Part 2

18/11/2010

TFS API Part 31 – Working With Queries – Part 2

In my last post TFS API Part 30 – Working With Queries I demonstrate a simple API example for getting Queries from TFS 2010, in this sample I’ll show how to run Queries and display the results, Deleting and Creating new Folder using TFS API.

image

Download Demo Project

Step 1: Add Results

Using WorkItemStore object and call the Query method you can set the results directly to DataGrid.

void item_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    TreeViewItem item = (TreeViewItem)sender;
    QueryDefinition q_def = (QueryDefinition)item.Tag;
    string q = q_def.QueryText;
    txt_query.Text = q;

    ///Repleace all @ because this code runs outside of Visual Studio environment.
    dataGrid1.ItemsSource = Store.Query
        (q.Replace("@project", "'" + ProjInfo.Name + "'").
        Replace("@Me", "'" + Environment.UserName + "'"));
    dataGrid1.UpdateLayout();
}

Step 2: Add and Remove – Queries and Folders

private void btn_add_Click(object sender, RoutedEventArgs e)
{
    if(Queries.SelectedItem != null && !string.IsNullOrEmpty(textBox1.Text))
    {
        TreeViewItem tree_item = (TreeViewItem)Queries.SelectedItem;
        QueryItem q_item = (QueryItem)tree_item.Tag;

        if (q_item.GetType() == typeof(QueryFolder))
        {
            QueryFolder folder = q_item as QueryFolder;
            folder.Add(new QueryFolder(textBox1.Text));
            textBox1.Text = string.Empty;
            BuildQueryHierarchy(Store.Projects[ProjInfo.Name].QueryHierarchy);
        }
        else
        { MessageBox.Show("Cannot Add Folder Under Query Definition"); }
    }
}

private void btn_remove_Click(object sender, RoutedEventArgs e)
{
    if (Queries.SelectedItem != null)
    {
        TreeViewItem tree_item = (TreeViewItem)Queries.SelectedItem;
        QueryItem q_item = (QueryItem)tree_item.Tag;

        if (q_item.GetType() == typeof(QueryFolder))
        {
            QueryFolder folder = q_item as QueryFolder;
            folder.Delete();                   
        }
        else if (q_item.GetType() == typeof(QueryDefinition))
        {
            QueryDefinition query_def = q_item as QueryDefinition;
            query_def.Delete();
        }

        BuildQueryHierarchy(Store.Projects[ProjInfo.Name].QueryHierarchy);
    }
}
Download Demo Project
Add comment
facebook linkedin twitter email

Leave a Reply

4 comments

  1. WalkMan27/11/2010 ב 15:41

    Hi Mate –

    TF248021: You have specified a query string that is not valid when you use the query method for a flat list of work items. You cannot specify a parameterized query or a query string for linked work items with the query method you specified.

    I have tried a bit but can’t get around running a tree/linked query and display the results in a grid. Any help appreciated!

  2. Allen Fienberg03/12/2010 ב 22:27

    I’m trying to get an array of all of the ID returned by a team querie via the TFS API. Your sample is getting me closer and I much appreciate it. Hopefully I can figure out how to get into the actual values returned by a query.

    In my opinion, the dataGrid1.UpdateLayout(); in your sample abstracts how to work with the query results.

  3. Young, Bill29/03/2011 ב 23:59

    Great example. Took me a bit of searching to figure out why my changes were not reflected in TFS.

    We’re not calling .Save() on the QueryHierarchy.

  4. russweb27/04/2011 ב 00:27

    Same problem as Walkman.
    This code sample doesnt appear to work for heirachal queries…only flat ones.