Welcome to MSDN Blogs Sign in | Join | Help

TFS Data Source ASP.NET Control

While learning ASP.NET for developing the RTE for MSDN Forums I had to interact with multiple teams and provide them with status reports on the work items. Some of them were not using TFS yet for that code base. So I wanted a way to easily write a TFS report, send the url to all these teams.

 

Given my earlier TFS background and the capabilities I learnt exist in ASP world thought why not leverage these two and thus began this exercise to write a TFS Data Source ASP.NET control which I can drop into any ASP.NET web page, bind it to any existing ASP.NET controls to create a simple TFS report.

 

 

TFS Data Source ASP.NET Control Snippet [ Build this as a dll ].

 

[This code snippet is provided under the Microsoft Permissive License.]

 

using System;

using System.Collections;

 

using System.Collections.Generic;

using System.Collections.ObjectModel;

 

using System.ComponentModel;

using System.Text;

 

using System.Web.Configuration;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

using Microsoft.TeamFoundation.Client;

using Microsoft.TeamFoundation.WorkItemTracking.Client;

using Microsoft.TeamFoundation.WorkItemTracking;

using Microsoft.TeamFoundation.Server;

using System.Security.Permissions;

 

[assembly: SecurityPermission(SecurityAction.RequestMinimum)]

 

// TFS ASP.NET Controls

namespace TfsWebComponents

{

    /// <summary>

    ///  TFS Data Source Component

    /// </summary>

    [ToolboxData("<{0}:TfsDataSource runat=server></{0}:TfsDataSource>")]

    public class TfsDataSource : ObjectDataSource

    {

        public TfsDataSource()

        {

            this.TypeName = "TfsWebComponents.TfsServer";

        }

 

    }

 

    /// <summary>

    ///  TFS Data Source Component Implementation.

    /// </summary>

    public class TfsServer

    {

        private readonly String tfsServerUrl;

        /// <summary>

        /// Tfs Server

        /// </summary>

        private TeamFoundationServer tfsServer;

        /// <summary>

        /// Tfs Work Item Store

        /// </summary>

        private WorkItemStore tfsWorkItemStore;

 

        /// <summary>

        ///  Project service.

        /// </summary>

        private ICommonStructureService tfsProjectService;

 

        /// <summary>

        /// Security service

        /// </summary>

        private IGroupSecurityService tfsSecurityService;

 

        /// <summary>

        /// Tfs Server

        /// </summary>

        public TfsServer()

        {

            tfsServerUrl = WebConfigurationManager.ConnectionStrings["TfsServer"].ConnectionString;

            tfsServer = TeamFoundationServerFactory.GetServer(tfsServerUrl);

 

            tfsServer.Authenticate();

            tfsProjectService = (ICommonStructureService)tfsServer.GetService(typeof(ICommonStructureService));

            tfsSecurityService = (IGroupSecurityService)tfsServer.GetService(typeof(IGroupSecurityService));

            tfsWorkItemStore = (WorkItemStore)tfsServer.GetService(typeof(WorkItemStore));

        }

 

 

        /// <summary>

        ///  Get Work items for a given team project using the given stored query name.

        /// </summary>

        /// <param name="teamProjectName">Name of the TFS team project</param>

        /// <param name="storedQueryName">Name of the stored query in TFS</param>

        /// <returns>ReadOnlyCollection of WorkItem</returns>

        public ReadOnlyCollection<TfsWorkItem> GetWorkItemsByQuery(string teamProjectName, string storedQueryName)

        {

            Collection<TfsWorkItem> workItems = new Collection<TfsWorkItem>();

            Hashtable h = new Hashtable();

            h.Add("project", teamProjectName);

 

            foreach (StoredQuery sq in tfsWorkItemStore.Projects[teamProjectName].StoredQueries)

            {

                if (string.Compare(sq.Name,

                                   storedQueryName,

                                   true,

                                   System.Globalization.CultureInfo.CurrentCulture) == 0)

                {

                    WorkItemCollection myWitems = tfsWorkItemStore.Query(sq.QueryText, h);

                    workItems = this.GetWorkItems(myWitems);

                    break;

                }

            }

 

            h = null;

            return new ReadOnlyCollection<TfsWorkItem>(workItems);

        }

 

        /// <summary>

        ///  Get collection of TfsWorkItem given the original WorkItemCollection from TFS.

        /// </summary>

        /// <returns>Collection of TfsWorkItem</returns>

        private Collection<TfsWorkItem> GetWorkItems(WorkItemCollection myWitems)

        {

            Collection<TfsWorkItem> workItems = new Collection<TfsWorkItem>();

 

            foreach (WorkItem itm in myWitems)

            {

                // TfsWorkItem is a wrapper for the real TFS work item

                // It provides get methods to all work Item properties like AssignedTo, State etc.

                TfsWorkItem tfswi = new TfsWorkItem(itm);

 

                //If you want to do additional processing do it here.

 

                workItems.Add(tfswi);

            }

 

            return workItems;

        }

    }

}

 

[This code snippet is provided under the Microsoft Permissive License.]

 

In my next post let’s see how to use this to create a simple nice looking TFS report!

Published Monday, April 23, 2007 2:46 PM by kannans

Comments

# Using an ASP.NET TFS data source

Kannan Sundararajan has written a couple of posts on how to use TFS as an ASP.NET data source. If you

Wednesday, April 25, 2007 11:23 PM by Buck Hodges

# VSTS Links - 04/26/2007

Ben Ryan on An override for "Get Latest on Checkout". Buck Hodges on Orcas Beta 1 has shipped - summary...

Thursday, April 26, 2007 11:24 AM by Team System News

# TFS Reporting Simplified using TFS Data Source Control

Kannan wrote two great posts on how to create a TFS Data Source Control and how to create a TFS Report...

Wednesday, May 02, 2007 5:21 AM by Archi

# re: TFS Data Source ASP.NET Control

which namespace does the class TfsWorkItem come from?

Monday, June 11, 2007 3:23 PM by jedihutch

# re: TFS Data Source ASP.NET Control

As I metioned in the comments TfsWorkItem is my own wrapper class around the original WorkItem returned by TFS.

Wednesday, June 13, 2007 6:40 PM by kannans

# How to configure WIT OM on a web application?

I see various questions on configuration difficulties in using Workitem OM in web application. Unfortunately

Monday, July 09, 2007 1:07 PM by Naren's Blog

# Login dialog when using the Team Foundation Server API

nwc = new NetworkCredential(tfsuid, TFSPass, TFSDomain); tfs = new TeamFoundationServer(TFSServerUrl

Friday, February 06, 2009 4:22 PM by Suresh Behera
Anonymous comments are disabled
 
Page view tracker