HOWTO: Execute a command by Guid and Id from a Visual Studio add-in

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio .NET 2002
Date: May 2008   Microsoft Visual Studio .NET 2003
Updated: March 2013   Microsoft Visual Studio 2005
      Microsoft Visual Studio 2008
      Microsoft Visual Studio 2010
      Microsoft Visual Studio 2012

Visual Studio commands (EnvDTE.Command) are identified by a Guid (which identifies the owner Visual Studio package) and an Id (which identifies the command within the package). To make commands more user-friendly a Name is also provided for most (but not all) commands. The EnvDTE.Command class provides those three properties.

More Information

The automation model (EnvDTE) of Visual Studio provides an EnvDTE.DTE.ExecuteCommand method to execute commands given its command name. However, if the command has no name that method is useless. For this purpose the automation model provides this other method:

EnvDTE.DTE.Commands.Raise(Guid, Id, CustomIn, CustomOut)

where the Guid and Id are the input parameters identifying the command, CustomIn is an optional parameter to supply information to the command and CustomOut is a value returned by the command.

Visual Studio uses two Guids for its core commands that are detailed in the Visual Studio SDK documentation:

VSConstants.VSStd97CmdID (Guid: "{5EFC7975-14BC-11CF-9B2B-00AA00573819}"):

VSConstants.VSStd2KCmdID (Guid: "{1496A755-94DE-11D0-8C3F-00C04FC2AAE2}"):

The actual values of the enums are in the VSConstants.cs file of the Visual Studio SDK that you can browse through http://www.koders.com if you don't want to download and install the Visual Studio SDK.

The following code shows an add-in that when loaded shows the Solution Explorer toolwindow executing the View.SolutionExplorer command by name and the Class View toolwindow executing the View.ClassView command by Guid and Id:

Imports System
Imports Microsoft.VisualStudio.CommandBars
Imports Extensibility
Imports EnvDTE

Public Class Connect
   Implements IDTExtensibility2

   Private m_objDTE As DTE

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

      m_objDTE = CType(application, EnvDTE.DTE)

      Select Case connectMode

         Case ext_ConnectMode.ext_cm_AfterStartup

         Case ext_ConnectMode.ext_cm_Startup
            ' OnStartupComplete will be called
      End Select

   End Sub

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


   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

   Private Sub Initialize()

      Const VSStd97CmdID As String = "{5EFC7975-14BC-11CF-9B2B-00AA00573819}"
      Const ClassView As Integer = 599


         ' Show the Solution Explorer toolwindow using the command name

         ' Show the Class View toolwindow using the command Guid and Id
         ' Equivalent to ExecuteCommand("View.ClassView")
         m_objDTE.Commands.Raise(VSStd97CmdID, ClassView, Nothing, Nothing)

      Catch objException As Exception
      End Try

   End Sub

End Class

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