In this Visual Studio Add-In, there are integrated search options for Visual Studio IDE. This AddIn works fine with Visual Studio 2005, 2008, 2010, 2012. Custom Search, Shortcut key Alt+F1.
Introduction
Visual Studio provides one of the most powerful IDEs on the market. And it also allows us to extend the functionalities. Visual Studio Add-ins are a 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.
Note: This add-in/plugin has been used and recommended by many Visual Studio experts.
Background
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 . Whenever I got a doubt about 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 to 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 -> Extensibility -> 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 with 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 these 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
has 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 .AddIn Files
Make sure that both the file contents are the same as below:
Step 12. Modify OnConnection method (Connect.cs)
Now the code will be:
Add the below codes to OnConnection
method:
Step 13. Add Method CreateContextMenu That’s Already Referred in OnConnection
Step 14. Add the NameSpace Microsoft.VisualStudio.CommandBars
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 System.Web Reference
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
In 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.
CreateContextMenu Method
The below given line specifies the window names where the WebSearch menus are available:
Then I loop through all the specified menus and call the AddWebSearch
by passing the CommandBar
object.
AddWebSearch Method
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.
oControl_Click
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 web page.
Here ctrl.tag
is the web url and the selected text is appended after encoding.
getSelectedText
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 the 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.
AddIn File
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 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 the comments section.
You can uninstall this from the Uninstall a Program Section or Add/Remove Program section in the control panel.
Add-In Action
The above image shows the working of WebSearch Addin in the Error List window.
In the 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 the 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 your choice.
Please check WebSearch official page for latest updates.
Troubleshooting
Check out my article on Troubleshooting Visual Studio Add-ins, Error Numbers and its 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 in all of my articles. Please don't forget to mark your votes, suggestions and feedback to improve the quality of this and upcoming articles.
History
- 2nd September, 2012: Initial version