Over a million developers have joined DZone.

Extending Team Explorer 2012 – Detecting Connection and using MVVM [2 of N]


Before moving on with other things we may wish to extend, let’s upgrade the trivial addin from the previous post. Since we are integrating with Team Explorer, it would be great to know to which project we are currently connected. If we are aware of the current connection, we can hide the navigation item. Once we regain connection we simply show it again. Since this requires us that we update the UI, and as you’ve seen the UI requests data from us, you will see how MVVM is used in the navigation item to alert Team Explorer pane that something has changed in our UI.

I have created a brand new VSIX project and library named Example2.VSExtension and Example2.ContextSensitiveNavigationItem respectively. I will do this for every blog post since this way I will not merge two separate concepts in one addin. Add a new class to the project, in my case it is named ContextSensitiveNavigationItem, and inherit from ITeamExplorerNavigationItem. Also add the necessary attribute and implement the interface in the same fashion as seen in the previous post. In order to be aware of the current TFS context, we need additional references. Add references to the following assemblies:

  • Microsoft.TeamFoundation.Client
  • Microsoft.Visualstudio.Shell.Immutable.10.0
  • System.ComponentModel.Composition

We can get the information from the service named ITeamFoundationContextManager. To get services in general inside Visual Studio, you need to use MEF and first import the service provider. Once you have it, you can ask it to give you the context manager. The following code demonstrates how to use the service provider:

public SVsServiceProvider ServiceProvider { get; set; }
private ITeamFoundationContextManager _contextManager;

public void Invalidate()
    if (_contextManager == null && ServiceProvider != null)
        _contextManager = ServiceProvider.GetService(typeof(ITeamFoundationContextManager)) as ITeamFoundationContextManager;
        if (_contextManager != null)
            _contextManager.ContextChanged += _contextManager_ContextChanged;

void _contextManager_ContextChanged(object sender, ContextChangedEventArgs e)

private void UpdateText(ITeamFoundationContext teamFoundationContext)
    if (teamFoundationContext == null || string.IsNullOrEmpty(teamFoundationContext.TeamProjectName))
        Text = string.Empty;
        Text = "Plugin connected to " + teamFoundationContext.TeamProjectName;

When we aren’t connected to the server at all, we should hide the navigation item since it probably doesn’t make sense to have it yet.


The code above caches the context manager inside a class field. Every time Invalidate is called, it will attempt to cache it if it wasn’t already. It will also register an event handler for the event which will fire every time you connect to or disconnect from a TFS project. This way we can be sure that the current project is changed. Changing the caption is a little bit complicated. If there is no caption, e.g. when we are not connected to any project, we also need to hide the navigation item. We also need to fire PropertyChanged whenever we change a property so that Team Explorer can update the UI. When we toggle visibility or change the caption, we need to inform TE that it is changed and that it needs to get the latest version.

Here is the full code for both properties:

private string _text;
private bool _isVisible;

public bool IsVisible
    get { return _isVisible; }
        if (_isVisible == value)
        _isVisible = value;

public string Text
    get { return _text; }
        if (_text == value)

        _text = value;
        IsVisible = !string.IsNullOrEmpty(_text);

We need to keep a backing field for the property and fire the event if the setter detects that the new value is different from the old value. The implementation of the missing function is given below.

private void RaisePropertyChanged(string property)
    if (PropertyChanged != null)
        PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(property));

Once we connect to the project, the navigation item is shown and the caption contains project name.

This simple addin demonstrates how to detect the current team explorer context and to act on it. We will need the current context in lots of cases. It also demonstrates how to use the built in MVVM model that Team Explorer relies on. In the next post I will show how to add new pages to Team Explorer.

Entries in this series:
  1. Extending Team Explorer 2012 - Introduction [0 of N]
  2. Extending Team Explorer 2012 - Adding navigation item [1 of N]
  3. Extending Team Explorer 2012 - Detecting connection and using MVVM [2 of N]


Published at DZone with permission of Toni Petrina, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}