Alessandro Del Sole's Blog

/* A programming space about Microsoft® .NET® */
posts - 159, comments - 0, trackbacks - 0

My Links

News

Your host

This is me! This space is about Microsoft® .NET® and Microsoft® Visual Basic development. Enjoy! :-)

These postings are provided 'AS IS' for entertainment purposes only with absolutely no warranty expressed or implied and confer no rights.

Microsoft MVP

My MVP Profile

I'm a VB!

Watch my interview in Seattle

My new book on VB 2015!

Pre-order VB 2015 Unleashed Pre-order my new book "Visual Basic 2015 Unleashed". Click for more info!

My new book on LightSwitch!

Visual Studio LightSwitch Unleashed My book "Visual Studio LightSwitch Unleashed" is available. Click the cover!

Your visits

Follow me on Twitter!

CodePlex download Download my open-source projects from CodePlex!

Article Categories

Archives

Post Categories

.NET Framework

Blogroll

Help Authoring

Microsoft & MSDN

Setup & Deployment

Visual Basic 2005/2008/2010

Deploying IntelliSense code snippets for Visual Studio 2012 with VSIX packages

Back in Visual Studio 2005, Microsoft introduced reusable code snippets that can be picked up in the code editor with IntelliSense support. Something like this:

Code Snippets are XML files with .snippet extension based on a specific schema. Rather than typing manually your XML files, a good idea is using a snippet editor. There are a lot of snippet editors available for free, I personally use the Snippet Designer editor which is integrated in the IDE. With an editor like this, you simply write or copy-paste code and provide the appropriate metadata information. The editor is responsible for producing well-formed XML files that can be correctly recognized by Visual Studio.

I will not cover creating code snippets here, I will instead show how to package snippets for easy deployment.

Once you have your snippets, you might want to use them in the code editor or to share them with other developers. Code snippets are stored inside language-specific folders, such as C:\Program Files (x86)\Microsoft Visual Studio 11.0\VB\Snippets\1033. Also, Visual Studio provides the My Code Snippets folders where users can save snippets without being administrators.

In the previous editions of Visual Studio, you could package a number of snippet files (plus add-ins, macros, item/project templates, controls) inside a .Vsi installer. Then, the Visual Studio Content Installer tool helped users install contents into the appropriate location.

Even though .Vsi packages are still supported, Visual Studio 2012's extensibility relies on the VSIX file format, which provides a better customer experience. For this reason, sharing reusable code snippets should be done with a VSIX package.

Assuming you have a number of snippets ready and that you already installed the Visual Studio 2012 SDK, the first thing you need to do is creating a new, empty VSIX project:

In Solution Explorer, add a new folder to the project where you will store snippets. The name of the folder is very important, because it will be also the name of the group of snippets inside the code editor. Add all the .snippet files you want to deploy to other developers:

As you can see in the figure, it is very important to remember to set the Build Action, Copy to Output Directory, and Include in VSIX properties as shown, otherwise snippets will not be included in the package.

The next step is adding a package definition file, with .pkgdef extension. To add one, you can simply use the text file template. The package definition file will tell the installer where to install code snippets. I am using Visual Basic code snippets, so I'm going to use the following syntax:

[$RootKey$\Languages\CodeExpansions\Basic\Paths]
"DelSoleVBSnippets"="$PackageFolder$"

What the code does, is finding the registry key that contains the collection of directories for VB code snippets on the target machine. To this collection, a new path is added with the custom key specified on the second line. Supported language identifiers are Basic, CSharp, SQL, HTML, XML, C++, and JavaScript. You can check out the MSDN documentation for additional strings in the pkgdef file.

Don't forget to set the file properties as you already did for snippets. The next step is making a manual edit to the extension manifest. In Solution Explorer, right click the source.extension.vsixmanifest file and choose to open it with the XML internal editor, finally add the following node before the closing file tag:

  <Assets>
    <Asset Type="Microsoft.VisualStudio.VsPackage" Path="Snippets\DelSoleVBSnippets.pkgdef" />
  </Assets>

Close the XML editor and re-open the manifest with its designer. Set the extension's metadata (publisher, license, images) but make sure you do not change the asset added manually. You are done.

You can now debug your extension in the experimental instance of Visual Studio by simply pressing F5. When ready, switch to the Release configuration and build the solution. You will find the redistributable .Vsix package under Bin\Release as usual. This package can be easily published to the Visual Studio Gallery for easy deployment through the Extension Manager in Visual Studio.

Note: if you plan to install your snippets to a non-default location, you might need to create a snippet index file. Check out this page in the MSDN documentation.

Enjoy your code snippets!

Alessandro

Print | posted on martedì 21 maggio 2013 09:40 | Filed Under [ Visual Studio 2012 ]

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 7 and 2 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET