Using Code Snippets for common K2 coding
Using code snippets in Visual Studio is nothing new but I have found it useful recently for storing sample code that I am likely to re use in my K2 workflows. I thought I would give the simple steps involved in setting up your own code snippets, it may save you a few key strokes or dare I say coding errors sometime!
Create your code snippet file
a) The code snippet file is simply an XML document with a .snippet extension containing your code and some descriptive elements. Therefore open up your favourite XML editor (notepad for me :-)) and create a document with the following outline:
<CodeSnippets
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>
First Snippet
</Title>
</Header>
<Snippet>
<Code Language="CSharp">
<![CDATA[Your Code]]>
</Code>
</Snippet>
</CodeSnippet>
<CodeSnippet Format="1.0.0">
<Header>
<Title>
Second Snippet
</Title>
</Header>
<Snippet>
<Code Language="CSharp">
<![CDATA[Your Code]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
b) Add your snippet title in between the title tags and your code in the ![CDATA[]] section as shown above
c) Save your file with a .snippet extension
Import your snippet into Visual Studio
If you know the directory VS is using for snippets you can just copy your file to it, if not then
- Open Visual Studio
- Click ctrl+k then ctrl+b and the snippets dialog will show, click import and select your snippet and click open
- Select the location to store the snippet and click finish
Use your code snippet
Any time you’re in a code block simply right click and choose Insert Snippet and select your snippet