all things Sitecore related

Create a Visual Studio add-in with contextmenu and selected text as input

By on February 25, 2010 in C#, Controls, Visual Studio with 0 Comments

Create a Visual Studio add-in with contextmenu and selected text as input

When working with a new way of storing settings in a database. I was frustrated how much work it was to check the value of setting from code. So I deceided to make my life a bit easier by creating a VS2008 contextmenu add-in. With this add-in I can select text within VS and use the value of the selected text within the add-in popup. The hardest part was figuring out how to create a contextmenu and how to use the selected text as input value.

In this blogpost I will show how to create a Visual Studio contextmenu add-in and pass the selected text to the pop-up. I’m not going to explain how to create an add-in you can easily find articles about this on MSDN or blogs (just try Google).

Now let’s get started. Create an new Visual Studio add-in project and add the following code to the OnConnetion Method within the Connect.cs. This code will insert add the contextmenu.

_applicationObject = (DTE2)application;
CommandBars cBars = (CommandBars)_applicationObject.CommandBars;

CommandBar editorCommandBar = (CommandBar)cBars["Editor Context Menus"];
CommandBarPopup editPopUp = (CommandBarPopup)editorCommandBar.Controls["Code Window"];

Command command = commands.AddNamedCommand2(_addInInstance,
 "GetSetting", "Bekijk Setting", "Executes the command for test", true, 733, ref contextGUIDS,
 (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,

Then to get the selected text I use the following method within the Exec of the Connect.cs and pass the selected text (return value) to a property of a Windows Form pop-up.

private string GetSelection()
    string setting = "";

    //Check active document
    if (_applicationObject.ActiveDocument != null)
        //Get active document
        TextDocument objTextDocument = (TextDocument)_applicationObject.ActiveDocument.Object("");
        TextSelection objTextSelection = objTextDocument.Selection;

        if (!String.IsNullOrEmpty(objTextSelection.Text))
 //Get selected text
            setting = objTextSelection.Text;
    return setting;

Hope it helps.


Tags: , ,

About the Author

About the Author: Pieter is Technical Marketing Manager for Sitecore Netherlands and owner of He has more than ten years experience with software developing in multiple programming languages and with different Content Management Systems. Before joining Sitecore Pieter was a lead developer for multiple Sitecore and .Net projects, he joined Sitecore in 2011 as an Solution Architect in The Netherlands, after two years as an Solution Architect he joined the Technical Marketing department. In the role as Techinical Marketing Manager he is responsible for the Global MVP program and the Sitecore technical branding strategy. You can follow Pieter on twitter: @pieterbrink123 or Google+ .


If you enjoyed this article, subscribe now to receive more just like it.

Post a Comment

Your email address will not be published. Required fields are marked *