Book Image

Visual Studio 2013 Cookbook

Book Image

Visual Studio 2013 Cookbook

Overview of this book

Table of Contents (17 chapters)
Visual Studio 2013 Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Creating Visual Studio add-ins and extensions


When Microsoft released Visual Studio 2010, they changed the approach to extensibility by introducing the VSIX format. The number of extensions in the Visual Studio gallery is a testament to how successful this change has been.

So, what do you do if you want to make your own add-ins and extensions in Visual Studio? This recipe will walk you through that process, though the magic that happens inside the add-in or extension is up to you.

Getting ready

To create extensions, you will need the Visual Studio 2013 SDK (http://www.microsoft.com/en-us/download/details.aspx?id=40758) and the Professional or higher version of Visual Studio, which you can download from Microsoft.

Once the SDK is installed, start Visual Studio 2013 and you're ready to go.

How to do it...

Perform the following steps:

  1. Start a new project by navigating to Other Project Types | Extensibility | Visual Studio Add-in and the default name.

  2. The Add-in Wizard will be launched. Click on Next on the first page.

  3. Choose the language you want to develop in. For the recipe, choose Create an Add-in using Visual C# and click on Next.

  4. Click on Next to choose the default Application Host (which will default to Microsoft Visual Studio 2013).

  5. Provide a name and description for the add-in if you are feeling creative, otherwise just click on Next.

  6. Click on Next to use the defaults for the add-in options, click on Next again to skip 'Help About' Information, and finally click on Finish. The wizard will generate an application skeleton including all of the references necessary for building Visual Studio 2013 add-ins. At this point, you can add the details of your add-in, but for this recipe, we will continue without it.

  7. Pressing F5 will start a new instance of Visual Studio in the debug mode (it may take a while to start) where you can use the add-in manager (Tools | Add-in Manager…) to enable your add-in and check its functionality. For this recipe, leave the add-in as it is.

  8. Add a new project to your solution by right-clicking on the solution in the Solution Explorer and add a new project using the Editor Viewport Adornment template by navigating to Visual C# | Extensibility, leaving the name as the default. If this option isn't available, check if you have installed the Visual Studio SDK correctly.

    Tip

    The VSIX file is not in the same location as the template used in step 1. You will find the VSIX in AppendixVSAddin1\ViewportAdornment1\source.extension.vsixmanifest, while the add-in project file is located in AppendixVSAddin1\AppendixVSAddin1\ if you are using the default names.

  9. Open the source.extension.vsixmanifest file and populate the Author field with your name.

  10. Set the new extension project as the default startup project (by right-clicking on the project in the Solution Explorer and clicking on Set as StartUp Project) and press F5 to start debugging. A new instance of Visual Studio will be launched using the experimental hive. The experimental hive is a separate set of Visual Studio settings you can use when testing extensions that won't affect your normal development settings. (You will notice that you are prompted to set your settings, just as you did the first time you launched Visual Studio 2013 after installing it.)

    Tip

    Because a debugger is attached, starting the experimental instance of Visual Studio may take longer than you are used to. It will have Experimental Instance in the title bar to help distinguish it from your regular instance of Visual Studio.

  11. When Visual Studio has finished loading, it will automatically instantiate the extension, making it active and available. From the Visual Studio menu, select File | New | File..., then select General | Text File and click on Open. You should see a purple box at the top-right corner of the editor surface, as shown in the following screenshot, proving that the extension is working as expected.

  12. Close the experimental instance of Visual Studio.

How it works...

There's a big difference between add-ins and extensions in Visual Studio. Add-ins are the old way of extending Visual Studio and are fairly complex to build, whereas Extensions are the preferred approach from Visual Studio 2010 onwards and are much easier to implement.

An add-in needs to implement an extensibility interface, and while this means your add-in can work with Visual Studio versions prior to 2010, it is limited to functionality exposed by the DTE interfaces, and as a developer you need to deal with a number of COM interfaces. An extension, on the other hand, needs to implement a Managed Extensibility Framework (MEF) contract and is not restricted in the API's it can access or in the way it is implemented.

There's more…

There is a lot more flexibility in building extensions over add-ins and this also applies to the update and distribution mechanism. However, if you really need to get to the internals of Visual Studio, or if you need to support Visual Studio 2008 or earlier, then you will need to look at the add-in approach.

The Extension Manager complements the NuGet package system described in the Managing packages with NuGet recipe in Chapter 4, .NET Framework 4.5.1 Development. The difference is that Extension Manager focuses on enhancements to Visual Studio, while NuGet is used to obtain libraries that you will be distributing with your application.

Tip

When you complete the recipe, if you want to remove every trace of your add-in from Visual Studio remove the .addin file from Documents\Visual Studio 2013\Addins.