Second Life of a Hungarian SharePoint Geek

January 14, 2010

Why don’t you create your own code snippet library for SharePoint?

Filed under: SharePoint, VSX — Tags: , — Peter Holpar @ 01:28

Code snippets provide a simple yet effective tool in the VSX (Visual Studio Extensibility) that can save you from a lot of typing and help to code faster.

You can create your own code snippet library for SharePoint, either for yourself or for novice colleagues to help their work while mastering SharePoint development.

If you need general information about working and creation of code snippets, you will find a lot of useful content at the following links:

Code Snippets

Code Snippets Schema Reference

Creating and Using IntelliSense Code Snippets

You can create and edit code snippets using Notepad like me, but you can use any XML editor, and there is a dedicated tool as well, called Snippy.

I suggest you to create your snippets in a dedicated folder, for example, I’ve created my code snippet library in the C:\Program Files\Microsoft Visual Studio 9.0\VC#\Snippets\1033\SharePoint folder. You should register this location in Visual Studio using the Code Snippets Manager (Tools / Code Snippets Manager…) as illustrated below.

image

I’ll illustrate the process of creating a SharePoint code snippet library on a simple example that is one of the SharePoint best practices: the using clause in the Coding Techniques to Ensure Object Disposal.

We want our code to be surrounded by the following using clauses:

  1. using (SPSite site = new SPSite(url))
  2. {
  3.     using (SPWeb web = site.OpenWeb())
  4.     {
  5.       
  6.     }
  7. }

To achieve this result, I’ve created the following XML and saved it into the code snippet folder created earlier using the file name getweb.snippet.

  1. <?xml version="1.0" encoding="utf-8" ?>
  2.   <CodeSnippetsxmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  3.     <CodeSnippet Format="1.0.0">
  4.         <Header>
  5.             <Title>getweb</Title>
  6.             <Shortcut>getweb</Shortcut>
  7.             <Description>Code snippet for getting a web reference</Description>
  8.             <Author>Peter Holpar</Author>
  9.             <SnippetTypes>
  10.                 <SnippetType>SurroundsWith</SnippetType>
  11.             </SnippetTypes>
  12.         </Header>
  13.         <Snippet>
  14.             <Declarations>
  15.                 <Literal>
  16.                     <ID>siteIdentifier</ID>
  17.                     <ToolTip>Identifier that represents the SPSite object</ToolTip>
  18.                     <Default>site</Default>
  19.                 </Literal>
  20.                 <Literal>
  21.                     <ID>webIdentifier</ID>
  22.                     <ToolTip>Identifier that represents the SPWeb object</ToolTip>
  23.                     <Default>web</Default>
  24.                 </Literal>
  25.                 <Literal>
  26.                     <ID>url</ID>
  27.                     <ToolTip>URL of the site</ToolTip>
  28.                     <Default>url</Default>
  29.                 </Literal>
  30.             </Declarations>
  31.             <Code Language="csharp"><![CDATA[using (SPSite $siteIdentifier$ = new SPSite($url$))
  32. {
  33.     using (SPWeb $webIdentifier$ = $siteIdentifier$.OpenWeb())
  34.     {
  35.         $selected$ $end$
  36.     }
  37. }]]>
  38.             </Code>
  39.         </Snippet>
  40.     </CodeSnippet>
  41. </CodeSnippets>

Since we want our code snippet to surround existing code, we use the value SurroundsWith as SnippetType.

Let’s see our code snippet in action!

We create the bulk of our method as shown below.

  1. public void ProcessWeb(String url)
  2. {
  3.     web.Title = "MyWeb";
  4.     web.Update();
  5. }

Then we select the two code lines of the method, right-click on the selection, and choose Surround With… from the pop-up menu. Then we choose SharePoint as the code snippet library and the getweb code snippet.

image

The next picture shows the result after the code snippet is applied to the selection.

image

I hope that this sample was able to demonstrate the power of code snippets and you can use this tool to make your work more effective, as you will be able to concentrate on tasks that really require your IQ instead of bothering with typing the same code blocks over and over again.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: