One of the reasons why Visual Studio is such a great development environment is its fluidity when it comes to customization. In my previous post, Creating a Visual Studio Extension Part 1, we discussed the preparation process for creating a Visual Studio Extension.
In this post, I will explain the basic steps needed to create a very simple Visual Studio Extension to access text from the currently opened Editor on Visual Studio.
If you have not installed the Visual Studio SDK, see my previous Blog post.
Creating Your First Project
Once the setup is complete, we are ready to get started. First, start your Visual Studio Version and create a new project.
Next select VSIX Project under Extensibility, choose a name and location for your new extension and press ok.
When done correctly, this is what you should see in Visual Studio 2015:
You should also get this nice Getting Started page with some useful information, as shown below.
If you are using Visual Studio 2013, you will start right in the VSIX Manifest Designer.
As soon as you are done reading the Getting Started page, go ahead and close it. You can read it again later by right clicking on the index.html file in the Solution Explorer and selecting open in browser.
Now that we’re ready to create our extension, let’s start with some minor cosmetics.
Double click on the source.extension.vsixmanifest file to open it. You should see the VSIX Manifest Designer for your vsixmanifest file. Note this is already open in Visual Studio 2013.
In this Designer you can edit metadata, install targets, assets and dependencies for your Extension.
The Designer allows you to enter a description for your Extension, pick an icon and add some release notes in order to let users know what they are using.
For now, we can just leave these fields blank, but remember to come back and add some useful information before deploying your extension.
Adding functionality to the Extension
Now it is time to add some functionality to your extension.
Right click on your project and select Add and then New Item. Next, navigate to C# and Extensibility.
Select the CustomCommand item, give it a name, and click the Add Button.
A new class will be created for you and some code will be generated, but don’t change anything yet.
Debugging your Visual Studio Extension
Save everything and start your new CustomCommand Extension for the first time by pressing F5.
Your solution should build and the new Extension will start to debug in an Experimental Visual Studio instance.
An Experimental Visual Studio instance is a new instance of Visual Studio without any of your settings. All changes in this instance are not saved, so you don’t have to worry about destroying your settings by accident. See more Information about Experimental Instances
The first time you start your Experimental Instance you will see the Welcome Window. You only have to do this once. Just press “Not now, maybe later” pick a color theme and start the new Instance of Visual Studio.
Go to the Tools menu in Visual Studio and you should see your Custom Command as Invoke CustomCommand.
Clicking on it will show you a Message Box with some text.
Closing the Experimental Instance should stop your Visual Studio Debug run.
Adding some custom code
Go back to your “real” Visual Studio instance and scroll down in your CustomCommand class (mine is named CustomCommand.cs).
You should find a Method Called MenuItemCallback.
This is the method that is executed when you click on the menu item in the Experimental Instance.
This code was generated by Visual Studio when you first created your class. So let us change it up a bit.
For this short How-To we will create some simple logic.
When our CustomCommand is executed in Visual Studio, the text from the currently active document in Visual Studio will be displayed in a message box.
First, we get a top level object from the Visual Studio instance. It is important to make sure it is not null.
Next, we get the currently active document and make sure that is not null.
We make sure it is a TextDocument and cast it.
Finally, we will see the text from the document from the start to the end.
And all that’s left is to just display that text.
If everything is correct, this is what our new Method will look like:
Replace the old method MenuItemCallback in your Extension with this new one and debug your Extension again.
Running your Visual Studio Extension
In your new Experimental Instance of Visual Studio, create a new text file and open it in the editor.
Enter some text in your new text file.
Click on Tools and select Invoke CustomCommand to start your CustomCommand.
You should see a Messagebox with the text from your currently active editor as text.
Congratulations! You’ve created your first Visual Studio CustomCommand Extension.
This is just a very simple example of what your can do by creating your own Visual Studio Extensions.
Now get creative and customize it.
If you have any questions about Visual Studio, or would like to learn more about how Visual Studio can help you to develop your own Extensions, don’t hesitate to contact us using the form below. We’d be happy to help!