Paul Hacker's Blog

Musings from a "Hacker"
posts - 21, comments - 36, trackbacks - 22

My Links

News



Archives

Access Stored Queries in Team Foundation Server

Have you ever had a need to take advantage of the StoredQueriesCollection in TFS? I was recentlly working on a project where I wanted to access a WorkItem query that I had created in my TFS project rather than create a SQL query string. To accomplish this I needed to access the StoredQueriesCollection for the TFS project that I was working with. Here is a snippet of code that will get the job done.

//connect to TFS

TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer(ConfigurationManager.AppSettings["TFS_SERVER"],new UICredentialsProvider());
            tfs.EnsureAuthenticated();

//get the current user that is logged in
            tfsuser = tfs.AuthenticatedUserName;

            WorkItemStore store = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));

//Get my project
                Project tfsProject = store.Projects [ConfigurationManager.AppSettings["TFS_PROJECT"]];

//Get the Stored Queries

                StoredQueryCollection sqc = tfsProject.StoredQueries;

                string querystring;

                foreach (StoredQuery query in sqc)
                {
                    if (query.Name == "All My Open Tasks")
                    {

//Get the query text that I will need to run
             

 querystring = query.QueryText;

//replace the @project variable inthe query with the name of my project

                        querystring = querystring.Replace("@project", "'" + tfsProject.Name + "'");

                        WorkItemCollection wic = store.Query(querystring);

//loop through the WorkItems that the query returns

                        foreach (WorkItem wi in wic)
                        {

//Loop through the fields and getthe data that I need

                            foreach (Field field in wi.Fields)
                            {
                                string Title = field.Value.ToString();

                                string startDate = (DateTime)field.Value;
                                string finishDate = (DateTime)field.Value;

                            }

                        }
                    }
                }

I could have very easily wrote a SQL query string, but I wanted to show how you take advantage of the StoredQueriesCollection. Hope you find this useful.

 

-pjhacker

Print | posted on Friday, August 04, 2006 8:31 AM |

Feedback

Gravatar

# re: Access Stored Queries in Team Foundation Server

This is very nice tip

But you could avoid doing the manual replacement if you used the this overload

Query( BatchReadParameterCollection batchReadParams, string wiql)

and pass into the batchReadParams variable the value for the macro
@project this will avoid manual replacements. somewhat cleaner.
1/21/2007 5:19 PM | Tiago Pascoal
Gravatar

# re: Access Stored Queries in Team Foundation Server

Hi,

I was wondering if you know of a way to get just the user's stored queries. So you have queries that are available for everyone to see, then those that are only viewable by the person who created them...is there a way to distinguish those?
4/24/2008 3:48 PM | Jason
Gravatar

# re: Access Stored Queries in Team Foundation Server

Never mind, I found that by checking the QueryScope of each item, you can determine if the query is "Public" or "Private".
4/24/2008 4:09 PM | Jason

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 3 and 6 and type the answer here:

Powered by: