HOWTO: Add a filtered task with navigation to the Task List from a Visual Studio add-in

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

Visual Studio 2005 and higher provides a Task List window (View, Other Windows, Task List menu) to show tasks such as "TODO" comments or user tasks.

Note: In Visual Studio 2005 and higher, compiler errors are shown in a separate Error List window (View, Error List menu), while in Visual Studio .NET 2002/2003 they appeared in the Task List. To add errors to the Error List see HOWTO: Add an error with navigation to the Error List from a Visual Studio add-in.

This article shows how to add a task to the Task List from a Visual Studio 2005 or higher add-in, how filter the Task List to view the task and how to provide automatic navigation to the file and line specified in the task.

More Information

The automation model (EnvDTE) of Visual Studio provides the EnvDTE.TaskList class to represent the Task List. You can get an instance of this class as explained in the following article:

HOWTO: Get the programmable inner object of a toolwindow

Its TaskItems property provides the collection of tasks, and the Add method. Although this Add method has parameters for the file and line associated to the task, it doesn't provide navigation capabilities. In other words, double clicking the task won't open the file and won't show the line in the code. You would have to handle the TaskListEvents.TaskNavigated event. Fortunately, the automation model of Visual Studio 2005 and higher provides the EnvDTE80.TaskItems2 class (To get the EnvDTE80.TaskItems2 instance, just cast TaskList.TaskItems to that type) with a new Add2 method that provides a new last AutoNavigate boolean parameter that when set to True, will provide the navigation automatically.

To get more information about the EnvDTE80.dll assembly, see the following article:

INFO: Assemblies used in Visual Studio Extensibility

The Task List provides a combobox to filter the tasks that appear in the list (such as "Comments", "User Tasks", etc.). When you add a task to the Task List from an add-in (or macro), a new filter value is used with the name "Add-ins and macros" and unless you filter the list by that value, the task won't be shown. Visual Studio doesn't provide a named command to execute to filter the list by a value, but it provides an unnamed one (using Guid and Id). See the following article:

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

The following add-in adds when loaded a new task to the Task List associated to the line 1 of the first file of the first project of the solution (ensure that there is a solution loaded when loading the add-in):

Imports SystemImports 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 AddTaskToList()

      Const VSStd2KCmdID_GUID As String = "{1496A755-94DE-11D0-8C3F-00C04FC2AAE2}"

      Const TaskListProviderCombo As Integer = 2200
      Const VIEW_ADD_INS_AND_MACROS As String = "Add-ins and Macros"

      Dim objWindow As EnvDTE.Window
      Dim objTaskList As TaskList
      Dim colTaskItems2 As EnvDTE80.TaskItems2
      Dim objProjectItem As ProjectItem
      Dim sFileName As String


         ' Get the TaskList window and make it visible
         objWindow = m_objDTE.Windows.Item(EnvDTE.Constants.vsWindowKindTaskList)
         objWindow.Visible = True

         ' Get the task items collection
         objTaskList = CType(objWindow.Object, EnvDTE.TaskList)
         colTaskItems2 = DirectCast(objTaskList.TaskItems, EnvDTE80.TaskItems2)

         ' Get the first file of the first project of the solution
         objProjectItem = m_objDTE.Solution.Projects.Item(1).ProjectItems.Item(1)
         sFileName = objProjectItem.FileNames(0)

         ' Add a task for that that file and line 1 with autonavigation (last parameter)
         colTaskItems2.Add2("", "", "My Task", vsTaskPriority.vsTaskPriorityHigh, Nothing, False, sFileName, 1, True, True, True)

         ' Filter the task list window by "Add-ins and macros" to view the new task
         m_objDTE.Commands.Raise(VSStd2KCmdID_GUID, TaskListProviderCombo, VIEW_ADD_INS_AND_MACROS, Nothing)

      Catch objException As Exception
      End Try

   End Sub

End Class

Related articles

Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this