Posts
415
Comments
233
Trackbacks
42
Creating Snippets For Visual Studio

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.

<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

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.

<Header>
  <Title> 
    MySnippet
  </Title>
  <Description>Inserts my favorite code</Description>
  <Author>Tim Murphy</Author>
  <Shortcut>myshortcut</Shortcut>

</Header>

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.

  <Declarations>
   <Literal>
    <ID>
     Var
    </ID>
    <ToolTip>
     Replace with your variable name
    </ToolTip>
    <Default>Var</Default>
   </Literal>
   <Object>
    <ID>
     Url
    </ID>
    <ToolTip>
     Replace with the Web URL
    </ToolTip>
    <Type>string</Type>
    <Default>URL</Default>
   </Object>
  </Declarations>

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 ($).

<Code Language="CSharp">
  <![CDATA[
              using (SPSite $Var$ = new SPSite($Url$))
                 {
                 }
   ]]>
</Code>

 

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 …

posted on Thursday, October 15, 2009 3:08 PM Print
Comments
No comments posted yet.

Post Comment

Title *
Name *
Email
Comment *  
 

Tim Murphy

Tim is a Solutions Architect for PSC Group, LLC. He has been an IT consultant since 1999 specializing in Microsoft technologies. Along with running the Chicago Information Technology Architects Group and speaking on Microsoft and architecture topics he was also contributing author on "The Definitive Guide to the Microsoft Enterprise Library".



I review for the O'Reilly Blogger Review Program



Technorati Profile

www.flickr.com
Tag Cloud