Adding a command to the Source Control Explorer context menu

Update 2/26/2009: You can find a version of this sample addin that works with VS 2005 here.  

We've gotten some questions recently about how to interact with and extend our out-of-the-box version control functionality. For most users, the main hub of activity is the Source Control Explorer, so it makes sense for us to provide some sample code demonstrating how to add a command to the Source Control Explorer's context menu. The sample additionally provides an add-in command framework making it easier to add commands to other parts of the version control user interface, such as the History window.

The new command added to the Source Control Explorer context menu in this sample is called "Multi Label" and is an extension of the "Quick Label" functionality provided in the downloadable Team Foundation Power Tools. With "Multi Label" you can label multiple items at once by selecting them in the Source Control Explorer's right pane. Once the Multi Label dialog is up, you can add more paths to label by typing them in.

Here's how to build and try out the add-in. 
 
1. On a machine with Visual Studio 2008 and Team Explorer installed, download and unzip VersionControlExtensibility.zip. Open the VersionControlExtensibility.sln file in Visual Studio.

2. Open the References node for the project in Solution Explorer, and see if any of the assemblies listed have the Warning icon next to them. Microsoft.VisualStudio.TeamFoundation.VersionControl.dll is referenced and is not in the GAC, so you may need to remove and re-add this reference since the path you find it in varies. On my machine (64-bit), the assembly is in C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies.extensibility1

    
3. Build the solution -- you should end up with the VersionControlExtensibility.dll file in the bin/ directory.

Now you're ready to install. Add-ins live in your My Documents\Visual Studio 2008\Addins folder on disk, and you'll need to drop 2 files in that directory:

1. The VersionControlExtensibility.AddIn file (which is just an XML file with information about the add-in DLL)
2. The VersionControlExtensibility.dll file which you just built.

Restart Visual Studio 2008 if you have it open, and the add-in will be loaded. Right-click on any item in the Source Control Explorer and you should see "Multi Label..." on the list.

extensibility2image

To add your own custom command to the SCE context menu, subclass SCEAddInCommand and provide implementations for the following properties.

// From AddInCommand -- base class for all commands
public override String Name { get { return "MyCommandName"; } }
public override String ButtonText { get { return "Text for Context Menu..."; } }
public override String ToolTip { get { return "A short description of my command"; } }
public override bool UseMSOIcon { get { return false; } } // Can be used to provide an icon for your command
public override int IconIndex { get { return -1; } } // Can be used to provide an icon for your command

// From SCEAddInCommand -- derived from AddInCommand; base class for commands hooking the SCE context menu
public override int SCEContextMenuPosition { get { return 5; } }

You’ll also need implementations for the following methods from AddInCommand.

// Called by Visual Studio to determine whether your command is enabled, disabled, or invisible.
public override void QueryStatus(EnvDTE.vsCommandStatusTextWanted neededText, ref EnvDTE.vsCommandStatus status, ref object commandText)
{
status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
}

// Called by Visual Studio when your command is clicked.
public override void Exec(EnvDTE.vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
handled = true;
MessageBox.Show(“Hi!”);
}

Last, you’ll need to add your custom command in Connect.cs’s OnConnection method so that it’s created when the add-in is loaded. I’ve left a sample line there you can uncomment and modify to fit the name of your AddInCommand.

Thanks to Ed Hintz for designing the original version of this framework.

VersionControlExtensibility2008.zip