Fast-Track Dynamics 365 Part 1 of 2 - Development with Visual Studio Snippets

Dominic Jarvis, 16 March 2018

When developing, there are often things that you repeat on a regular basis. In order to speed up development, it can be helpful to have portions of code that are available for quick access.

With Visual Studio, it is possible to put together code snippets with templated variables, which allows for quick creation of essential portion of code. For example, when developing CRM solutions, I very frequently find myself writing Query Expressions that are very similar in format. So I’ve created a template of these to use, which can be activated very simply.

Setting Up

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets
     xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
   <CodeSnippet Format="1.0.0">
     <Header>
       <Title>QueryExpression</Title>
       <Description>Adds basic query expression template</Description>
       <Shortcut>qe</Shortcut>
     </Header>
     <Snippet>
       <Code Language="CSharp">
         <![CDATA[QueryExpression $qe$ = new QueryExpression("$logicalName$") { NoLock = true };

            EntityCollection $coll$ = sdk.RetrieveMultiple($qe$);$end$]]>
       </Code>
       <Declarations>
         <Literal>
           <ID>qe</ID>
           <ToolTip>Replace with the name of the QueryExpression.</ToolTip>
           <Default>qe</Default>
         </Literal>
         <Literal>
           <ID>coll</ID>
           <ToolTip>Replace with the name of the EntityCollection.</ToolTip>
           <Default>coll</Default>
         </Literal>
         <Literal>
           <ID>logicalName</ID>
           <ToolTip>Replace with the logical name of the entity to retrieve.</ToolTip>
           <Default>account</Default>
         </Literal>
       </Declarations>
     </Snippet>
   </CodeSnippet>
</CodeSnippets>



A snippet is very quick and easy to set up. A complete guide can be found here: https://docs.microsoft.com/en-us/visualstudio/ide/walkthrough-creating-a-code-snippet but I’ll provide a quick overview.

  1. Create a new xml file with the name of your snippet. I called mine QueryExpressionSnippet.
  2. Replace the extension with .snippet – this lets Visual Studio know that this is a snippet file and should be treated as one. If you are developing the snippet in VS, this is good as VS will provide intellisense for the available xml options.
  3. Fill in the snippet with the required information. I’ll post my snippet definition below.
  4. Open the Code Snippets Manager (Tools > Code Snippets Manager)
  5. Click Import
  6. Navigate to and open previously created snippet
  7. Choose snippet location – this should be My Code Snippets by default
  8. Use your snippet!

Snippet Definition

In the definition, the attributes of the snippet are set. This includes the Title/name, description and designated shortcut.

Templated variables can be created in the declarations node and then included by surrounding them with ‘$’ characters, as shown above. The variables can then be customized under ‘Declarations’, to specify the ID, Tooltip, and Default. 
Note: $end$ is added to remove the extra line break at the start of the snippet.

Use

Once set up, the snippet is very easy to use. Simply type the specified shortcut – in this case qe – and press the tab key to expand the snippet.

image

image

Once used, the templated variables can be tabbed through and changed just like out of the box Visual Studio code snippets.
  image