DCSIMG
How To: Write Team System Custom Control - Shai Raiten's Blog

Shai Raiten's Blog

It's all about code...

How To: Write Team System Custom Control

How To: Write Team System Custom Control

In this tutorial I'll show how to create simple Team System Custom Control.

Create new project:

Add reference to:
(Version 9.0.0.0 for Visual Studio 2008)

  • Microsoft.TeamFoundation.WorkItemTracking.Controls
  • Microsoft.TeamFoundation.WorkItemTracking.Client

You can find those dll's in C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies

Add using

using Microsoft.TeamFoundation.WorkItemTracking.Controls;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

Add new User Control and lable.

image

 

public partial class Header : UserControl, IWorkItemControl

    {

        private System.Collections.Specialized.StringDictionary properties;

 

        private bool readOnly = false;

 

        public Header()

        {

            InitializeComponent();

        }

 

        #region IWorkItemControl Members

 

        /// Raise this events before updating WorkItem object with values. When value is changed by a control, work item form asks all controls (except current control) to refresh their display values (by calling InvalidateDatasource) in case if affects other controls

        public event EventHandler BeforeUpdateDatasource;

 

        /// Raise this event after updating WorkItem object with values. When value is changed by a control, work item form asks all controls (except current control) to refresh their display values (by calling InvalidateDatasource) in case if affects other controls

        public event EventHandler AfterUpdateDatasource;

 

        /// Control is asked to clear its contents

        void IWorkItemControl.Clear()

        {

        }

 

        /// Control is requested to flush any data to workitem object. This usually happens during save operation or when the form is left. In most cases data will be written to workitem immediately on change and hence this will not need implementation. Some customers want a way to do operations during save, and this is the closest thing we got. If you do need a way to react to before-save & after-save events, pls let us know in forums given below and we'll consider for future revision.

        void IWorkItemControl.FlushToDatasource()

        {

            this.BeforeUpdateDatasource(this, EventArgs.Empty);        

            this.AfterUpdateDatasource(this, EventArgs.Empty);

        }

 

        /// Asks control to invalidate the contents and redraw. At this point, control can read from work item object and display/refresh data.

        void IWorkItemControl.InvalidateDatasource()

        {

            try

            {

                string WitType = m_workItem.Type.Name;

                label1.Text = WitType;

                switch (WitType)

                {

                    case "Bug":

                        this.BackColor = Color.Blue;

                        break;

                    case "Task":

                        this.BackColor = Color.Brown;

                        break;

                    case "Requirement":

                        this.BackColor = Color.Pink;

                        break;

                    default:

                        this.BackColor = Color.White;

                        break;

                }

            }

            catch (Exception ex)

            {

            }

        }

        /// A property bag of all attributes specified in work item form xml for this control. Custom attributes are allowed and can be used to pass parameters specific for this control from work item type xml.

        System.Collections.Specialized.StringDictionary IWorkItemControl.Properties

        {

            get { return properties; }

            set { properties = value; }

        }

 

        /// Tells the control to display in readonly mode.

        bool IWorkItemControl.ReadOnly

        {

            get { return readOnly; }

            set { readOnly = value; OnReadOnlyChanged(); }

        }

 

        /// Gives pointer to IServiceProvider if you intended to access Document service or VS Services. If services are not needed, do nothing in this method.

        private IServiceProvider m_serviceProvider;

        void IWorkItemControl.SetSite(IServiceProvider serviceProvider)

        {

            m_serviceProvider = serviceProvider;

        }

 

        private WorkItem m_workItem;

        object IWorkItemControl.WorkItemDatasource

        {

            get

            {

                return m_workItem;

            }

            set

            {

                m_workItem = (WorkItem)value;

            }

        }

 

        /// The field name if the control is associated with a field name in work item form xml. A custom control can be associated with 0 or 1 work item field.

        private string m_fieldName;

        string IWorkItemControl.WorkItemFieldName

        {

            get

            {

                return m_fieldName;

            }

            set

            {

                m_fieldName = value;

            }

 

        }

        #endregion

 

        private bool IsInitialized()

        {

 

            return (!this.IsDisposed &&

                m_workItem != null &&

                !string.IsNullOrEmpty(m_fieldName) &&

                m_workItem.Fields.Contains(m_fieldName));

        }

 

        private void OnReadOnlyChanged()

        {

        }

    }

 

Custom Control

You can download the Project from Here

Comments

Shai Raiten said:

How To: Manage Custom Controls In Team System and Web Access Here is couple of question I'm hearing

# October 22, 2008 3:28 PM

Team System News said:

Lou Vega on Minor "Gotcha!" TFS 2008 Workgroup Edition Shai Raiten on Team System "Virtual" User Group...

# October 23, 2008 9:00 AM

mario aguero said:

where do I see the control?

I cant find it or see such screen of yours

# October 31, 2008 7:08 PM

Shai Raiten said:

Hello,

I just add a User Control and add a Lable.

The code changes the Text on that lable according the Work Item Type.

You can download the demo project and see.

If you have any more questions please let me know.

# November 1, 2008 6:03 AM

AMohamed said:

your demo project don't have any project that show how i can add your custom control anywhere...

# November 25, 2008 3:42 AM

Shai Raiten said:

# November 25, 2008 4:26 AM

TFSBeginer said:

How i can add your custom control to any work item in my process template????.....

# November 25, 2008 4:35 AM

Shai Raiten said:

How To: Add Custom Controls Into Work Item I had question regarding the post about How To: Write Team

# November 28, 2008 2:23 PM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# November 29, 2008 4:30 AM

vstf wanabe said:

I tried compiling your project and I got: SR.CustomControls.Header does not implement interface member Microsoft.TeamFoundation.WorkItemTracking.Controls.IWorkItemControl.WorkItemFieldName.

How do I get around this error?

# August 6, 2009 6:50 PM

Shai Raiten said:

Hi,

First you can impliment this interface, second what version of visual studio is installed on your computer?

# August 7, 2009 10:57 AM

vstf wanabe said:

Visual Studio Team System 2008 Development Edition

Version 9.0.30729.1 SP

.net framework 3.5 SP1

I downloaded the code from your website and did not make any modifications.

# August 7, 2009 5:02 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: