Philip, a dev on version control, recently helped with a question on how to get the TFS objects we use in our UI.  I thought I’d post since others may find it useful.

We recently had a request from a customer for a VS add-in that would be able to access the same TfsTeamProjectCollection and VersionControlServer objects that our own UI integration (such as the Team Explorer and Pending Changes toolwindow) are using. In this particular case the customer wanted to hook the BeforeCheckinPendingChange event from the VersionControlServer object and take a specific action when that occurred. But the framework shown in this piece of sample code is generic -- you can use it to get the very same VersionControlServer or WorkItemStore object that our integration is using to connect to TFS.

The trick here is to hook the ProjectContextChanged event on the TeamFoundationServerExt extensibility object. While that extensibility point won't give you the TfsTeamProjectCollection object directly, we can ask the TfsTeamProjectCollectionFactory's static GetTeamProjectCollection method to retrieve it from a runtime cache. The cache is keyed by URI -- which (handily) is provided by TeamFoundationServerExt. By the time the ProjectContextChanged event fires, the ActiveProjectContext.DomainUri property has already been updated.

All the services in the TFS client object model are owned by the TfsTeamProjectCollection. Once we have it, we can call GetService to request the VersionControlServer object. There's only one per TfsTeamProjectCollection; the same holds true for WorkItemStore, IBuildServer, or any of the other client object model services you may be familiar with.

Happy extending!

using System;
using System.Diagnostics;
using System.Windows.Forms;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using Microsoft.TeamFoundation.Common;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Common;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.VisualStudio.TeamFoundation;
using Microsoft.VisualStudio.TeamFoundation.VersionControl;

namespace MyAddin1
{
    /// <summary>The object for implementing an Add-in.</summary>
    /// <seealso class='IDTExtensibility2' />
   
public class Connect : IDTExtensibility2
    {
        /// <summary>Implements the constructor for the Add-in object. Place your initialization code within this method.</summary>
       
public Connect()
        {
        }

        /// <summary>Implements the OnConnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being loaded.</summary>
        /// <param term='application'>
Root object of the host application.</param>
        /// <param term='connectMode'>
Describes how the Add-in is being loaded.</param>
        /// <param term='addInInst'>
Object representing this Add-in.</param>
        /// <seealso class='IDTExtensibility2' />
       
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
        {
            _applicationObject = (DTE2)application;
            _addInInstance = (AddIn)addInInst;

            try
           
{
                m_tfsExt = _applicationObject.GetObject("Microsoft.VisualStudio.TeamFoundation.TeamFoundationServerExt") as TeamFoundationServerExt;

                if (null != m_tfsExt)
                {
                    m_tfsExt.ProjectContextChanged += new EventHandler(m_tfsExt_ProjectContextChanged);

                    if (null != m_tfsExt.ActiveProjectContext)
                    {
                        // Run the event handler without the event actually having fired, so we pick up the initial state.
                       
m_tfsExt_ProjectContextChanged(null, EventArgs.Empty);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>Implements the OnDisconnection method of the IDTExtensibility2 interface. Receives notification that the Add-in is being unloaded.</summary>
        /// <param term='disconnectMode'>
Describes how the Add-in is being unloaded.</param>
        /// <param term='custom'>
Array of parameters that are host application specific.</param>
        /// <seealso class='IDTExtensibility2' />
       
public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
        {
            // Unhook the ProjectContextChanged event handler.
           
if (null != m_tfsExt)
            {
                m_tfsExt.ProjectContextChanged -= new EventHandler(m_tfsExt_ProjectContextChanged);
                m_tfsExt = null;
            }
        }

        /// <summary>Implements the OnAddInsUpdate method of the IDTExtensibility2 interface. Receives notification when the collection of Add-ins has changed.</summary>
        /// <param term='custom'>
Array of parameters that are host application specific.</param>
        /// <seealso class='IDTExtensibility2' />       
       
public void OnAddInsUpdate(ref Array custom)
        {
        }

        /// <summary>Implements the OnStartupComplete method of the IDTExtensibility2 interface. Receives notification that the host application has completed loading.</summary>
        /// <param term='custom'>
Array of parameters that are host application specific.</param>
        /// <seealso class='IDTExtensibility2' />
       
public void OnStartupComplete(ref Array custom)
        {
        }

        /// <summary>Implements the OnBeginShutdown method of the IDTExtensibility2 interface. Receives notification that the host application is being unloaded.</summary>
        /// <param term='custom'>
Array of parameters that are host application specific.</param>
        /// <seealso class='IDTExtensibility2' />
       
public void OnBeginShutdown(ref Array custom)
        {
        }

        /// <summary>
        ///
Raised by the TFS Visual Studio integration package when the active project context changes.
       
/// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
       
private void m_tfsExt_ProjectContextChanged(Object sender, EventArgs e)
        {
            try
           
{
                if (null != m_tfsExt.ActiveProjectContext &&
                    !String.IsNullOrEmpty(m_tfsExt.ActiveProjectContext.DomainUri))
                {
                    SwitchToTfs(TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(m_tfsExt.ActiveProjectContext.DomainUri)));
                }
                else
               
{
                    SwitchToTfs(null);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void SwitchToTfs(TfsTeamProjectCollection tfs)
        {
            if (Object.ReferenceEquals(m_tfs, tfs))
            {
                // No work to do; could be a team project switch only
               
return;
            }

            if (null != m_tfs)
            {
                m_tfs.GetService<VersionControlServer>().BeforeCheckinPendingChange -= new ProcessingChangeEventHandler(VersionControlServer_BeforeCheckinPendingChange);
                m_tfs = null;
            }

            if (null != tfs)
            {
                m_tfs = tfs;
                m_tfs.GetService<VersionControlServer>().BeforeCheckinPendingChange += new ProcessingChangeEventHandler(VersionControlServer_BeforeCheckinPendingChange);               
            }
        }

        private void VersionControlServer_BeforeCheckinPendingChange(Object sender, ProcessingChangeEventArgs e)
        {
            if (null != e.PendingChange &&
                !String.IsNullOrEmpty(e.PendingChange.ServerItem))
            {
                MessageBox.Show("About to check in: " + e.PendingChange.ServerItem);
            }
        }
       
        private DTE2 _applicationObject;
        private AddIn _addInInstance;

        private TeamFoundationServerExt m_tfsExt;
        private TfsTeamProjectCollection m_tfs;
    }
}