Click here to Skip to main content
15,861,172 members
Articles / Programming Languages / C#

Web Search - Visual Studio Add-in, Search (Google, Yahoo, MSDN, Code Project, Stack Overflow and more...) for the Selected Text/Item

Rate me:
Please Sign up or sign in to vote.
4.98/5 (105 votes)
10 Mar 2014CPOL8 min read 262.6K   3.3K   124   132
Visual Studio Add-in, WebSearch - Multiple search options included in all text editor, reference items, error list.
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.

Image 1

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: Image 2 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 Smile | <img src= . 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

Image 4

Click “OK”.

Now the Visual Studio Add-in wizard will be shown in IDE.

Step 2: Click on “Next” Button

Image 5

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.

Image 6

Here, I have selected C#, as I’m more comfortable with this language.

Step 4. Select the Add-in Host

Image 7

Here, I selected Visual Studio 2010.

Step 5: Give the Name and Description for the Add-in

Image 8

I have given the Name and Description details for this Add-in as below.

Image 9

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.

Image 10

Step 6: Choose the Add-in Options

Image 11

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

Image 12

If you want to show Add-in details like below, then select the check box and enter the required data.

Image 13

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'

Image 14

Now Visual Studio has generated the code as per our inputs in the wizard.

Step 10. The WebSearch Solution Explorer will Look Like Below

Image 15

Step 11. Review the .AddIn Files

Image 16

Make sure that both the file contents are the same as below:

Image 17

Step 12. Modify OnConnection method (Connect.cs)

Now the code will be:

Image 18

Add the below codes to OnConnection method:

Image 19

Step 13. Add Method CreateContextMenu That’s Already Referred in OnConnection

Image 20

Step 14. Add the NameSpace Microsoft.VisualStudio.CommandBars

Image 21

Step 15. Add Method AddWebSearch that has Been Called from CreateContextMenu

Image 22

Step 16. Add the Context Menu Click Events

Image 23

Step 17. Add getSelectedText Method that’s Already Referred in Menu Click Event

Image 24

Step 18. Add System.Web Reference

Image 25

As we are using HttpUtility.urlEncode method, we need to refer to this assembly.

Added System.Web will be shown in 'Reference Items' folder:

Image 26

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:

Image 27

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:

Image 28

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:

Image 29

Then I loop through all the specified menus and call the AddWebSearch by passing the CommandBar object.

Image 30

AddWebSearch Method

This method is used to Add WebSearch menu to the specified context menus:

Image 31

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.

Image 32

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.

Image 33

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.

Image 34

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.

Image 35

If anything comes in selected item/text, then we are opening the default web browser to open the web page.

Image 36

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:

Image 37

If clicked from Reference Item, then the Solution Explorer section will handle the menu click.

Image 38

Otherwise, the default section will get evaluated:

Image 39

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.

Image 40

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.

Image 41

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.

Image 42

Add-In Action

Image 43

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.

WebSearch Visual Studio 2005, 2008, 2010, 2012 - Shemeer NS

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.

We bSearch - Shemeer NS

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:

Image 46

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

Image 47

Check out my article on Troubleshooting Visual Studio Add-ins, Error Numbers and its possible solutions.


This Is All About WebSearch, Now You Say...

Image 48

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
Technical Manager/ Software Architect | CodeProject MVP | Visual Studio Marketplace Contributor | Author | Geek | Netizen | Husband | ChessPlayer

Most of my articles are listed on top 5 of the respective 'Best articles of the month' and some of my articles are published on ASP.NET WebSite's Article of the Day section.

Check my contributions in Visual Studio Marketplace and Code Project

Technical Blog: https://shemeerns.wordpress.com/
Facebook: http://facebook.com/shemeernsblog
Twitter : http://twitter.com/shemeerns
Google+ : http://google.com/+Shemeernsblog

Comments and Discussions

 
GeneralRe: Doesn't work in VS2012 Pin
PeterA13-Feb-13 18:50
professionalPeterA13-Feb-13 18:50 
GeneralRe: Doesn't work in VS2012 Pin
Shemeer NS13-Feb-13 18:52
professionalShemeer NS13-Feb-13 18:52 
GeneralMy vote of 5 Pin
ChaseForNone1-Jan-13 21:58
ChaseForNone1-Jan-13 21:58 
GeneralRe: My vote of 5 Pin
Shemeer NS2-Jan-13 18:46
professionalShemeer NS2-Jan-13 18:46 
GeneralNice Pin
Nasir ahmed U9-Dec-12 22:05
Nasir ahmed U9-Dec-12 22:05 
GeneralRe: Nice Pin
Shemeer NS3-Feb-13 8:16
professionalShemeer NS3-Feb-13 8:16 
GeneralMy vote of 5 Pin
Sperneder Patrick22-Nov-12 2:45
professionalSperneder Patrick22-Nov-12 2:45 
GeneralRe: My vote of 5 Pin
Shemeer NS23-Nov-12 2:17
professionalShemeer NS23-Nov-12 2:17 
Thanks Sperneder...Thumbs Up | :thumbsup:
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA15-Oct-12 21:59
professionalȘtefan-Mihai MOGA15-Oct-12 21:59 
GeneralRe: My vote of 5 Pin
Shemeer NS16-Nov-12 6:51
professionalShemeer NS16-Nov-12 6:51 
QuestionGood tools Pin
ridoy15-Oct-12 3:44
professionalridoy15-Oct-12 3:44 
AnswerRe: Good tools Pin
Shemeer NS16-Nov-12 6:50
professionalShemeer NS16-Nov-12 6:50 
GeneralMy vote of 5 Pin
rspercy6514-Oct-12 10:52
rspercy6514-Oct-12 10:52 
GeneralRe: My vote of 5 Pin
Shemeer NS14-Oct-12 18:09
professionalShemeer NS14-Oct-12 18:09 
BugMSDN link is not working correctly Pin
MT_11-Oct-12 20:44
professionalMT_11-Oct-12 20:44 
GeneralRe: MSDN link is not working correctly Pin
Shemeer NS11-Oct-12 20:53
professionalShemeer NS11-Oct-12 20:53 
GeneralRe: MSDN link is not working correctly Pin
MT_11-Oct-12 21:36
professionalMT_11-Oct-12 21:36 
GeneralRe: MSDN link is not working correctly Pin
Shemeer NS12-Oct-12 1:30
professionalShemeer NS12-Oct-12 1:30 
QuestionNot working properly for me Pin
ScruffyDuck11-Oct-12 6:43
ScruffyDuck11-Oct-12 6:43 
AnswerRe: Not working properly for me Pin
Shemeer NS11-Oct-12 8:32
professionalShemeer NS11-Oct-12 8:32 
GeneralRe: Not working properly for me Pin
ScruffyDuck11-Oct-12 9:58
ScruffyDuck11-Oct-12 9:58 
GeneralRe: Not working properly for me Pin
Shemeer NS11-Oct-12 20:16
professionalShemeer NS11-Oct-12 20:16 
GeneralMy vote of 5 Pin
Lacy0010-Oct-12 21:34
Lacy0010-Oct-12 21:34 
GeneralRe: My vote of 5 Pin
Shemeer NS11-Oct-12 8:21
professionalShemeer NS11-Oct-12 8:21 
GeneralMy vote of 5 Pin
ravithejag8-Oct-12 23:25
ravithejag8-Oct-12 23:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.