So creating a code snippet is a relatively easy task, however I will make a real case to demonstrate the creation process (refresh your mind on the topic here):
- First create a text file (with your preferred editor, mine is Notepad) with a .snippet extension.
- Now open the saved text file (it will be actually an XML file) and after careful review of the Code Snippets Schema Reference ( http://msdn2.microsoft.com/en-us/library/ms171418.aspx ), we are ready to proceed further.
The file must provide a valid XML schema in the first line so we will begin by declaring:
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
[ code snippet content here]
</CodeSnippets>
Inside the CodeSnippets section we declare the snippet container as follows:
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
[ code snippet content here]
</CodeSnippet>
</CodeSnippets>
Then we proceed by declaring a header section
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Telerik sample snippet</Title>
<Shortcut>ItemCommand</Shortcut>
</Header>
[ code snippet content here]
</CodeSnippet>
</CodeSnippets>
* The shortcut element is used for easier insertion of the code snippet – type
"ItemCommand" and press Tab to insert the code snippet in a VB.NET project, or press Tab twice if in C# project.
And the real code snippet goes to the Snippet section:
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Telerik sample snippet</Title>
</Header>
<Snippet>
<References>
<Reference>
<Assembly>System.Windows.Forms.dll</Assembly>
</Reference>
</References>
<Code Language="VB">
<![CDATA[MessageBox.Show("Hello World")]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
There you can implement References section (only Visual Basic supports this section)
<Snippet>
<References>
<Reference>
<Assembly>System.Xml.dll</Assembly>
</Reference>
</References>
<Code Language=" CSharp">
</Code>
</Snippet>
…the Imports section which is very useful if one wants to include commonly used namespaces, say Telerik.WebControls, Telerik.WebControls.UI:
<Snippet>
<Imports>
<Import>
<Namespace>Telerik.WebControls</Namespace>
</Import>
<Import>
<Namespace> Telerik.WebControls.UI</Namespace>
</Import>
</Imports>
<Code Language=" CSharp">
</Code>
</Snippet>
* There must be exactly one Namespace element in an Import element
... and Code section ( in a specified language supported by the CLR). I will use code for a common scenario regarding the usage of Custom Item Commands in our r.a.d.grid component:
<Snippet>
<Imports>
<Import>
<Namespace>Telerik.WebControls</Namespace>
</Import>
<Import>
<Namespace> Telerik.WebControls.UI</Namespace>
</Import>
</Imports>
<Code Language=" CSharp">
<![CDATA[
protected void RadGrid1_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
{
if (e.CommandName == "EditSelected")
{
if (RadGrid1.SelectedIndexes.Count == 0)
{
return;
}
foreach (GridDataItem item in RadGrid1.SelectedItems)
{
item.Edit = true;
}
e.Item.OwnerTableView.Rebind();
return;
}
if (e.CommandName == "UpdateEdited")
{
if (RadGrid1.EditIndexes.Count == 0)
{
return;
}
foreach (GridDataItem item in RadGrid1.EditItems)
{
RadGrid1.MasterTableView.PerformUpdate(item, true);
}
e.Item.OwnerTableView.Rebind();
return;
}
if (e.CommandName == "DeleteSelected")
{
if (RadGrid1.SelectedIndexes.Count == 0)
{
return;
}
foreach (GridDataItem item in RadGrid1.SelectedItems)
{
e.Item.OwnerTableView.PerformDelete(item, true);
}
e.Item.OwnerTableView.Rebind();
return;
}
if (e.CommandName == "CancelAll")
{
foreach (GridDataItem item in RadGrid1.EditItems)
{
item.Edit = false;
}
e.Item.OwnerTableView.IsItemInserted = false;
e.Item.OwnerTableView.Rebind();
return;
}
}
]]>
</Code>
</Snippet>
(Supported languages are VB, CSharp, VJSharp, and XML). Also all snippet code must be placed between <![CDATA[ and ]]> brackets.
So finally here is the complete code:
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Telerik sample snippet</Title>
</Header>
<Snippet>
<Imports>
<Import>
<Namespace>Telerik.WebControls</Namespace>
</Import>
<Import>
<Namespace> Telerik.WebControls.UI</Namespace>
</Import>
</Imports>
<Code Language=" CSharp">
<![CDATA[
protected void RadGrid1_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
{
if (e.CommandName == "EditSelected")
{
if (RadGrid1.SelectedIndexes.Count == 0)
{
return;
}
foreach (GridDataItem item in RadGrid1.SelectedItems)
{
item.Edit = true;
}
e.Item.OwnerTableView.Rebind();
return;
}
if (e.CommandName == "UpdateEdited")
{
if (RadGrid1.EditIndexes.Count == 0)
{
return;
}
foreach (GridDataItem item in RadGrid1.EditItems)
{
RadGrid1.MasterTableView.PerformUpdate(item, true);
}
e.Item.OwnerTableView.Rebind();
return;
}
if (e.CommandName == "DeleteSelected")
{
if (RadGrid1.SelectedIndexes.Count == 0)
{
return;
}
foreach (GridDataItem item in RadGrid1.SelectedItems)
{
e.Item.OwnerTableView.PerformDelete(item, true);
}
e.Item.OwnerTableView.Rebind();
return;
}
if (e.CommandName == "CancelAll")
{
foreach (GridDataItem item in RadGrid1.EditItems)
{
item.Edit = false;
}
e.Item.OwnerTableView.IsItemInserted = false;
e.Item.OwnerTableView.Rebind();
return;
}
}
]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
Additional reading:
http://msdn2.microsoft.com/en-us/library/ms165396.aspx
http://msdn2.microsoft.com/en-us/library/wy5tazc9.aspx