How To: Write Team System Custom Control

19/10/2008

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

Add comment
facebook linkedin twitter email

Leave a Reply

7 comments

  1. mario aguero31/10/2008 ב 19:08

    where do I see the control?
    I cant find it or see such screen of yours

  2. Shai Raiten01/11/2008 ב 06:03

    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.

  3. AMohamed25/11/2008 ב 03:42

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

  4. TFSBeginer25/11/2008 ב 04:35

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

  5. vstf wanabe06/08/2009 ב 18:50

    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?

  6. Shai Raiten07/08/2009 ב 10:57

    Hi,

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

  7. vstf wanabe07/08/2009 ב 17:02

    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.