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

Dominic Jarvis, 19 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.

In a previous blog, I wrote about how you can use Visual Studio snippets to allow for quick creation of code  that is used on a regular basis.

Another way to accomplish this is through the use of Visual Studio item templates. A template, as opposed to a snippet, can allow you to create the overall structure of your code. A good example of when this is useful is when developing plugins for CRM. Often, plugins in CRM follow a certain structure or process:

•    Obtain IPluginExecutionContext
•    Obtain IOrganizationServiceFactory
•    Obtain IOrganizationService
•    Obtain needed parameters from context
•    Execute plugin logic

If you are following these steps the entire time, it doesn’t make sense to write them out manually each time. This is where templates come in. It’s possible to write the general structure of the file, including using statements and supplementary methods.

Setting up

Templates are quite quick to set up. A complete guide can be found here: but I’ll provide a quick overview. For the purposes of this example, I’ll demonstrate how to create a plugin template.

  1. Add an item to your project - C# class file.
  2. Modify the item to create the shell of the template - perform regular logic – anything that is generic and will be reused.
  3. Replace portions of code with templated variables – a guide for this can be found here: I’ve also provided an example below.
  4. In the Project menu, click Export Template.
  5. Choose Item Template.
  6. Select the item you just created to export – then choose next.
  7. Select the assembly references you want to include in the export – then choose next.
  8. Enter the template name, description, and whether you want to automatically import the created template, then click finish. The files for the template will be copied to the directory specified in the wizard, with the default directory being %USERPROFILE%\Documents\Visual Studio <version>\My Exported Templates.
  9. If you chose to automatically import the template, this will be available for use after restarting Visual Studio. If not, copy the template zip and place it in the item template folder for your user. The default location is %USERPROFILE%\Documents\Visual Studio <version>\Templates\ItemTemplates.
  10. The template is ready for use.

Template Definition

I’ve included the sample code for a plugin template here, which includes initialising the IOrganizationService and ITracingService, and obtaining an entity from the context.

using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace $rootnamespace$
     public class $safeitemname$ : IPlugin
         public void Execute(IServiceProvider serviceProvider)
             IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
             IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
             IOrganizationService service = factory.CreateOrganizationService(null);
             ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            Entity target = context.InputParameters["Target"] as Entity;

            // Logic goes here...


Once set up, the template is very easy to use. In a project, select add item, then select the template for use. The template will automatically draw the variables from the creation window, including name and namespace.

The end result is shown below: