Visual Studio provides one of the most powerful IDEs on the market. And it also allows us to extend the functionalities too. Visual Studio Add-ins are powerful way to do this.
In this article I’m going to create a Visual Studio Add-in named “Web Search”. Using this Add-in we can easily search online for a selected text from the editor window or an item from the error list/ reference folder. Multiple search engine options are available with this Add-in.
This WebSearch AddIn setup can be downloaded from Visual Studio Gallery also. Please check WebSearch official page for latest updates.
For your reference I have attached the working source code that's used in this article and setup(updated) for this Add-In.
I have put my time and efforts on all of my articles, Please don't forget to mark your votes, suggestions and feedback to improve the quality of this and upcoming articles.
Note : This add-in/plugin has been used and recommended by many visual studio experts.
How many times have you copied something from the Visual Studio environment and searched for that? It can be anything, like a simple keyword or an error item. As a developer I used to do this most of the time https://www.codeproject.com/script/Forums/Images/smiley_smile.gif . Whenever I get a doubt on something, I used to search for that online to know more.
When we look into the ‘Reference’ folder of a complex application we may find some unfamiliar reference items added, at this time we used to search for that online for a better understanding.
While doing development we used to get some compile time error. If it’s not known for us then the next step will be Google it.
This Add-in is created to increase our productivity by adding the search option to the VS IDE and Currently this AddIn setup supports Visual Studio 2005, 2008, 2010, 2012.
Steps for Creating WebSearch Add-In
Here I will explain step by step to create the WebSearch Visual Studio Add-in. The steps given are based on Visual Studio 2010 IDE and .NET Framework 4.0.
Code explanation is given at the end of this article.
Step 1. Create a new Visual Studio Add-in named “WebSearch2010”,
New Project -> Other Project Types -> Extensibilty -> Visual Studio Add-in
Click “OK” .
Now the Visual Studio Add-in wizard will be shown in IDE.
Step 2. Click on “Next” button,
There are six steps in this Wizard. We can configure the Add-in behavior and other configurations in these six steps.
Step 3. Select the programming language that will be used to develop the Add-in,
Here I have selected
C#, as I’m more comfortable on this language.
Step 4. Select the Add-in host,
Here I selected Visual Studio 2010.
Step 5. Give the Name and Description for the Add-in,
I have given the Name and Description details for this Add-in as below,
Once this Add-in is installed then this details will be shown in the Add-in manager like below,
Can be accessed from Tools Menu -> Add-in manager.
Step 6. Choose the Add-in options,
All the options are self-descriptive.
Here the first option will generate the code to create a menu under the tools menu. WebSearch have only context menu. And the second option will make sure the Add-in is loaded when the host application starts
Step 7. Choosing "Help About" Information,
If you want to show Add-in details like below, then select the check box and enter the required data.
If you wanted to change any of the input(s) then you can press the 'Back' button and change the required part.
Step 9. Review the Summary and click 'Finish',
Now Visual Studio has generated the code as per our inputs in the wizard.
Step 10. The WebSearch solution explorer will look like below,
Step 11. Review the .
Make sure that both the file contents are same as below,
Step 12. Modify
OnConnection method (
Now the code will be
Add the below codes to
Step 13. Add Method CreateContextMenu that’s already referred in
Step 14. Add the NameSpace
Step 15. Add Method AddWebSearch that has been called from CreateContextMenu
Step 16. Add the Context Menu Click events,
Step 17. Add getSelectedText method that’s already referred in menu click event,
Step 18. Add
As we are using
HttpUtility.urlEncode method we need to refer to this assembly.
Added System.Web will be shown in 'Reference Items' folder
Step 19. Now the WebSearch Add-in is completed and ready to test.
Make sure that the test Add-in file is located in the Addins folder of Visual Studio 2010 under the current user's personal data folder.
Press F5 to run Visual Studio in test mode and open a dummy project, and then test the context menu like below,
Step 20. Create a Deployment and Setup and then it’s ready for distributing to the developer community.
If required I will be posting an article later, on Setup and Deployment of Visual Studio Add-In.
How the code works.
OnConnection method I have called
CreateContextMenu method to add the context menu's to some specified windows
Here once the Add-in is loaded we are calling the method
CreateContextMenu to add the context menus
The below given line specifies the window names where the WebSearch menus are available
Then I’m looping through all the specified menus and calling the
AddWebSearch by passing the
This method is used to Add WebSearch menu to the specified context menus,
The above code block is used to add the WebSearch main menu.
If you want to add a separator line after the menu then add the below line.
I have created an anonymous type to hold the sub menu details, the anonymous type is given below,
as you can see all the attributes are self-descriptive.
After defining the sub menu list I’m looping through the sub menu list and adding the sub menus, I have added click handler to handle the menu clicks.
This event is used to handle all the sub menu clicks. The selected text/item value will be retrieved by the below code.
If anything comes in selected item/text then we are opening the default web browser to open the the web page.
Here ctrl.tag is the web url and the selected text is appended after encoding.
This method is used to get the value according the active window selected
If we are clicking the menu from “
Error List” then it will be handled in below code,
If clicked from
Reference Item then the
Solution Explorer section will handle the menu click,
Otherwise the default section will get evaluated ,
Please let me know if you find a better way to retrieve the selected value.
Make sure that the
Add-in files flags are properly set.
Using the attached files
Working codes are attached with this article, if you want you can alter the code as per your needs. The Installer can be used to install this Add-In to your local box.
In-case of a any exception, this Add-in will log an entry in the Event Log of windows with the exception details. If you find any issues please report in comment section.
You can uninstall this from the Uninstall a Program Section or Add/Remove Program section in the control panel.
The above image shows the working of WebSearch Addin in the
Error List window.
In latest Version of WebSearch Setup you have the option to configure the Search options. So by using configure option you can add or remove the web site urls as per your choice. The below given screen-shot shows how to configure WebSearch.
and also you can configure WebSearch's context menu location and shortcut key using this configure option. If the shortcut key is valid then it will be shown in the
Tools->Web Search menu. The below given image shows how to assign a shortcut key for WebSearch plugin.
The assigned shortcut key can be used to open a search key in a web url that's mentioned in default search option. Using this configure option you won't able to assign any shortcut keys that's been currently used for any other commands. This behavior of WebSearch makes sure that the shortcut key is valid and it's not removing any used shortcut keys by mistake.
If you want to use any existing keys like F1, then go to Tools->Option->Environment->Keyboard-> then select
WebSearch.connect.WebSearch and assign any keys as per your choice.
If the Add-in is not initialized properly due to previous installation or any other issues the WebSearch menu will not be shown under Tools menu, In this case you won't be able to select any shortcut keys for WebSearch as shortcut key section will be in disabled mode. To resolve this issue you must reset WebSearch Add-in using below command.
Open visual studio
command prompt, then type/execute the below command
devenv.exe /resetaddin WebSearch.Connect.WebSearch
You can open the search result in default web browser, Visual Studio Tab or any other Web Browser as per the configuration,
In the above image the Other Browser Drop-down-list is populated with all the available web browsers from your machine. If you want to open search result in other than default browser/visual studio then you can select any browser from the drop-down list as per you choice.
Please check WebSearch official page for latest updates
Checkout my article on Troubleshooting Visual Studio Add-ins, Error Numbers and it's possible solutions
This is all about WebSearch, Now you say...
In this article I have tried to explain How to create a Visual Studio Add-in named WebSearch with detailed steps, I hope you have enjoyed this article and got some value addition to your knowledge.
I have put my time and efforts on all of my articles, please don't forget to mark your votes, suggestions and feedback to improve the quality of this and upcoming articles.