Logo
HOWTO: Get the nodes in the Server Explorer from a Visual Studio add-in

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio 2005
Date: July 2013   Microsoft Visual Studio 2008
      Microsoft Visual Studio 2010
      Microsoft Visual Studio 2012
       
Introduction

This article provides a sample to get the nodes in the Server Explorer of Visual Studio from an add-in.

More Information

The Server Explorer is a toolwindow whose inner content can be retrieved locating the EnvDTE.Window and using it Object property. The returned object is of the type EnvDTE.UIHierarchy, which has a UIHierarchyItems property which allows to iterate recursively the nodes of the hierarchy. Each EnvDTE.UIHierarchyItem node, apart from a UIHierarchyItems property to get its children, has properties such as Name (to identify the node), Object (which is useless for the Server Explorer since it returns an EnvDTE.UIHierarchyItem type), IsSelected (to get if the node is selected) and a Select method that receives the selection kind (vsUISelectionTypeSelect for single selection, vsUISelectionTypeExtend to include the node in the current selection, etc.).

The following add-in, when loaded, shows the names of the nodes of the Server Explorer:

Language: C#   Copy Code Copy Code (IE only)
using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;

namespace MyAddin1
{
   public class Connect : IDTExtensibility2
   {
      private DTE2 _applicationObject;
      private AddIn _addInInstance;
      
      public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
      {
         _applicationObject = (DTE2)application;
         _addInInstance = (AddIn)addInInst;
         
         switch( connectMode)
         {
            case ext_ConnectMode.ext_cm_AfterStartup:

               // The add-in has been loaded after Visual Studio was loaded. Since Visual Studio
               // is fully initialized, we can initialize the add-in
               InitializeAddIn();
               break;

            case ext_ConnectMode.ext_cm_Startup:

               // The add-in has been loaded with Visual Studio. Do nothing until Visual Studio 
               // is fully initialized (the OnStartupComplete method will be called)
               break;

            case ext_ConnectMode.ext_cm_UISetup:

               // Do nothing in this case
               break;
         }
      }

      public void OnStartupComplete(ref Array custom)
      {
         InitializeAddIn();
      }

      private void InitializeAddIn()
      {
         EnvDTE.Window serverExplorerToolwindow;
         EnvDTE.UIHierarchy hierarchy;

         try
         {
            serverExplorerToolwindow = GetToolwindow(EnvDTE.Constants.vsWindowKindServerExplorer);

            if (serverExplorerToolwindow != null)
            {
               hierarchy = (EnvDTE.UIHierarchy)serverExplorerToolwindow.Object;

               ShowNodes(hierarchy.UIHierarchyItems);
            }
         }
         catch (Exception ex)
         {
            System.Windows.Forms.MessageBox.Show(ex.ToString());
         }
      }

      private EnvDTE.Window GetToolwindow(string windowKind)
      {
         EnvDTE.Window toolWindow = null;

         foreach (EnvDTE.Window window in _applicationObject.Windows)
         {
            if (window.ObjectKind == windowKind)
            {
               toolWindow = window;
               break;
            }
         }
         return toolWindow;
      }

      private void ShowNodes(UIHierarchyItems hierarchyItems)
      {
         foreach (UIHierarchyItem hierarchyItem in hierarchyItems)
         {
            System.Windows.Forms.MessageBox.Show(hierarchyItem.Name);

            // Enter in recursion
            ShowNodes(hierarchyItem.UIHierarchyItems);
         }
      }

      public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
      {
      }

      public void OnAddInsUpdate(ref Array custom)
      {
      }

      public void OnBeginShutdown(ref Array custom)
      {
      }
   }
}
Language: VB.NET   Copy Code Copy Code (IE only)
Imports System
Imports Extensibility
Imports EnvDTE
Imports EnvDTE80

Public Class Connect
   Implements IDTExtensibility2

   Private _applicationObject As DTE2
   Private _addInInstance As AddIn

   Public Sub OnConnection(ByVal application As Object, ByVal connectMode As ext_ConnectMode, _
        ByVal addInInst As Object, ByRef custom As Array) Implements IDTExtensibility2.OnConnection

      _applicationObject = CType(application, EnvDTE80.DTE2)
      _addInInstance = CType(addInInst, EnvDTE.AddIn)

      Select Case connectMode

         Case ext_ConnectMode.ext_cm_AfterStartup

            ' The add-in has been loaded after Visual Studio was loaded. Since Visual Studio
            ' is fully initialized, we can initialize the add-in
            InitializeAddIn()

         Case ext_ConnectMode.ext_cm_Startup

            ' The add-in has been loaded with Visual Studio. Do nothing until Visual Studio 
            ' is fully initialized (the OnStartupComplete method will be called)

         Case ext_ConnectMode.ext_cm_UISetup

            ' Do nothing in this case

      End Select

   End Sub

   Public Sub OnStartupComplete(ByRef custom As Array) Implements IDTExtensibility2.OnStartupComplete

      InitializeAddIn()

   End Sub

   Private Sub InitializeAddIn()

      Dim serverExplorerToolwindow As EnvDTE.Window
      Dim hierarchy As EnvDTE.UIHierarchy

      Try

         serverExplorerToolwindow = GetToolwindow(EnvDTE.Constants.vsWindowKindServerExplorer)

         If Not serverExplorerToolwindow Is Nothing Then

            hierarchy = CType(serverExplorerToolwindow.Object, EnvDTE.UIHierarchy)

            ShowNodes(hierarchy.UIHierarchyItems)

         End If

      Catch ex As Exception

         System.Windows.Forms.MessageBox.Show(ex.ToString())

      End Try

   End Sub

   Private Function GetToolwindow(ByVal windowKind As String) As EnvDTE.Window

      Dim toolWindow As EnvDTE.Window = Nothing
      Dim window As EnvDTE.Window

      For Each window In _applicationObject.Windows

         If window.ObjectKind = windowKind Then

            toolWindow = window
            Exit For

         End If

      Next

      Return toolWindow

   End Function

   Private Sub ShowNodes(ByVal hierarchyItems As UIHierarchyItems)

      Dim hierarchyItem As UIHierarchyItem

      For Each hierarchyItem In hierarchyItems

         System.Windows.Forms.MessageBox.Show(hierarchyItem.Name)

         ' Enter in recursion
         ShowNodes(hierarchyItem.UIHierarchyItems)
      Next

   End Sub

   Public Sub OnDisconnection(ByVal disconnectMode As ext_DisconnectMode, ByRef custom As Array) _
      Implements IDTExtensibility2.OnDisconnection

   End Sub

   Public Sub OnAddInsUpdate(ByRef custom As Array) Implements IDTExtensibility2.OnAddInsUpdate
   End Sub

   Public Sub OnBeginShutdown(ByRef custom As Array) Implements IDTExtensibility2.OnBeginShutdown
   End Sub

End Class

Related articles



Go to the 'Visual Studio Extensibility (VSX)' web site for more articles like this (Articles section)


Top