How custom wit control Open another work item in Team Explorer

For the steps how to create and apply custom control, please refer to How to:Create a Custom WI Control

Now we want to a feature to make custom wit control open another work item, like the behavior of links.

To create a Custom WI Contrl which could open another workitem, following references are needed: (for VS2008, use version 9.0.0.0)

EnvDTE(version 8.0.0.0)
EnvDTE100(version 10.0.0.0)
EnvDTE80(version 8.0.0.0)
EnvDTE90(version 9.0.0.0)
Extensibility(version 7.0.3300.0)


Microsoft.TeamFoundation(version 10.0.0.0)
Microsoft.TeamFoundation.Client(version 10.0.0.0) Microsoft.TeamFoundation.WorkItemTracking.Client(version 10.0.0.0)
Microsoft.TeamFoundation.WorkItemTracking.Control(version 10.0.0.0)
Microsoft.VisualStudio.TeamFoundation(version 10.0.0.0)
Microsoft.VisualStudio.TeamFoundation.WorkItemTracking(version 10.0.0.0)

The control UI is

image

The code is (Important is marked as red at the end)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.TeamFoundation.WorkItemTracking.Controls;
using Microsoft.VisualStudio.TeamFoundation;
using Microsoft.VisualStudio.TeamFoundation.WorkItemTracking;
using Microsoft.TeamFoundation.Client;
using EnvDTE80;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System.Collections.Specialized;

namespace CustomWitControl
{
    public partial class OpenWorkItem : UserControl, IWorkItemControl
    {
        public OpenWorkItem()
        {
            InitializeComponent();
        }

        #region IWorkItemControl Members

        private EventHandlerList m_events;

        private EventHandlerList Events
        {
            get
            {
                if (m_events == null)
                {
                    m_events = new EventHandlerList();
                }
                return m_events;
            }
        }

        private static object EventBeforeUpdateDatasource = new object();

        public event EventHandler BeforeUpdateDatasource
        {
            add { Events.AddHandler(EventBeforeUpdateDatasource, value); }
            remove { Events.RemoveHandler(EventBeforeUpdateDatasource, value); }
        }

        protected virtual void OnBeforeUpdateDatasource(EventArgs eventArgs)
        {
            EventHandler handler = (EventHandler)Events[EventBeforeUpdateDatasource];
            if (handler != null)
            {
                handler.Invoke(this, eventArgs);
            }
        }

        private static object EventAfterUpdateDatasource = new object();

        public event EventHandler AfterUpdateDatasource
        {
            add { Events.AddHandler(EventAfterUpdateDatasource, value); }
            remove { Events.RemoveHandler(EventAfterUpdateDatasource, value); }
        }

        void IWorkItemControl.Clear()
        {
            textBox1.Text = string.Empty;
        }

        void IWorkItemControl.FlushToDatasource()
        {            // nothing to do
        }

        void IWorkItemControl.InvalidateDatasource()
        {
        }

        private StringDictionary m_properties;

        StringDictionary IWorkItemControl.Properties
        {
            get
            {
                return m_properties;
            }
            set
            {
                m_properties = value;
            }
        }


        private bool m_readOnly;

        bool IWorkItemControl.ReadOnly
        {
            get
            {
                return m_readOnly;
            }
            set
            {
                m_readOnly = value;
            }
        }

        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;
            }

        }

        private string m_fieldName;

        string IWorkItemControl.WorkItemFieldName
        {
            get
            {
                return m_fieldName;
            }
            set
            {
                m_fieldName = value;
            }
        }






        #endregion

       
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                int id = int.Parse(textBox1.Text);
                System.Diagnostics.Debugger.Launch();
                GotoWorkItems(id);
               

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        EnvDTE80.DTE2 dte2;
        EnvDTE80.DTE2 DTE2
        {
            get
            {
                if (dte2 == null)
                {
                    var vs = m_serviceProvider.GetService(typeof(EnvDTE.DTE));
                        
                    dte2 = (EnvDTE80.DTE2)vs ;
                }
                return dte2;
            }
        }

        private TeamFoundationServerExt GetTFSExt()
        {


            TeamFoundationServerExt tfsExt = DTE2.GetObject("Microsoft.VisualStudio.TeamFoundation.TeamFoundationServerExt") as TeamFoundationServerExt;
            if ((tfsExt.ActiveProjectContext.DomainUri == null) || (tfsExt.ActiveProjectContext.ProjectUri == null))
            {
                MessageBox.Show("Error");
                return null;
            }
            return tfsExt;
        }

        private void GotoWorkItems(int id)
        {
            TeamFoundationServerExt tfsExt = this.GetTFSExt();
                            DocumentService witDocumentService = (DocumentService)DTE2.DTE.GetObject("Microsoft.VisualStudio.TeamFoundation.WorkItemTracking.DocumentService");
                TeamFoundationServer activeTFS = new TeamFoundationServer(tfsExt.ActiveProjectContext.DomainUri);


                IWorkItemDocument widoc = null;
                widoc = witDocumentService.GetWorkItem(activeTFS, id, this);
                try
                {
                    witDocumentService.ShowWorkItem(widoc);
                }
                finally
                {
                    widoc.Release(this);
                }

            //}
        }

    }
}

posted on 2009-12-10 17:02  Ruiz  阅读(1832)  评论(0编辑  收藏  举报

导航