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 s...
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 pac