There is nothing earth shattering here. I was just putting together a set of snippets to make life a little easier while developing SharePoint and Office Open XML projects. My search for information crossed a lot of different sites. MSDN has a number of articles about all the features of creating and managing snippets. Below is a compilation of the basics around creating these little helpers. Hopefully it is a little more detailed in the whys then MSDN content.
Creating A Snippet File
Snippet files are really just XML files with a .snippet extension. The main thing is the schema that is applied and the available tags. I have found a couple of namespaces listed for the CodeSnippets tag, but the following seemed to work most reliably.
Once you have that in place you can start working on the header information. These include your title, author, description and shortcut. Most of this shows up in the snippet manager, but the shortcut allows a developer to type the shortcut text and hit tab to invoke the snippet.
<Description>Inserts my favorite code</Description>
Now we get to the meat of the snippet. The snippet itself is broken into two parts: the declaration and the code.
The declaration allows you to define the parts of your snippet which the developer will need to replace with their own identifiers. Declarations themselves come in two flavors: literals and objects. While this should be pretty self-explanatory lets be thorough. Literals are just that. They are strings that can be inserted at the specific location. These are perfect for letting the developer insert URLs or string defaults in declarations. Objects on the other hand have a type specified for them.
For all declarations you are allowed to add a tooltip so the the developer understand the usage of your snippet. Below are examples of both kinds of declarations.
Replace with your variable name
Replace with the Web URL
Once you have the declarations defined you simply add you code as you would normally see it. The only catch is that you will need to create a token in the code for each declaration offset by dollar characters ($).
using (SPSite $Var$ = new SPSite($Url$))
Using The Snippet Manager
The snippet manager allows you to import your files and make them available within the Visual Studio environment. One thing it does not seem to allow you to do is delete individual snippet files. It has the option to delete a directory which is great as long as you use the Add feature which allows you to bring in an entire folder of snippets at one time. Otherwise all snippets added with the Import feature get put under the My Code Snippets folder.
And that about covers the basics. Good coding …