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

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


     xmlns=”http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet”>
  
    

      
       Adds basic query expression template
       qe
    

    
      
        

            EntityCollection $ coll$ = sdk.RetrieveMultiple($ qe$ );$ end$ ]]>
      
      
                    qe
           Replace with the name of the QueryExpression.
           qe
        
                    coll
           Replace with the name of the EntityCollection.
           coll
        
                    logicalName
           Replace with the logical name of the entity to retrieve.
           account
        
      

     </Snippet>
   </CodeSnippet>

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 thumb Fast Track Dynamics 365 Part 1 of 2   Development with Visual Studio Snippets

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

Once used, the templated variables can be tabbed through and changed just like out of the box Visual Studio code snippets.
  image thumb 2 Fast Track Dynamics 365 Part 1 of 2   Development with Visual Studio Snippets

Let’s block ads! (Why?)

Magnetism Solutions Dynamics CRM Blog