Best Practices when Writing Dynamics CRM Plugins

  • For improved performance, Microsoft Dynamics 365 caches plug-in instances. The plug-in’s Execute method should be written to be stateless because the constructor is not called for every invocation of the plug-in. Also, multiple system threads could execute the plug-in at the same time. All per invocation state information is stored in the context, so you should not use global variables or attempt to store any data in member variables for use during the next plug-in invocation unless that data was obtained from the configuration parameter provided to the constructor. Changes to a plug-ins registration will cause the plug-in to be re-initialized.
  • Its a best practice to check for the target entity name and message name at the beginning of the plugin execute message to avoid running the plugin unintentionally.
  • When you want to update fields on a record, it’s good practice to create a new entity or early bound type of the record and only add the fields you want to update. By only updating the fields you are changing you reduce triggering other plugins running needlessly.
  • When retrieving an entity using SDK, make sure you are instantiating a new object (not just assigning a reference) and assigning it to the returned object from the retrieve SDK message for better performance.
  • Do not update the retrieve Target entity because it will update all fields included in the target entity.  The primary entity targeted by a platform create or update event should not be updated within the context of plug-in execution.  Developers should instead design their plug-in to execute in a stage prior to the core operation and manipulate the target object in the InputParameters.
  • The common method to avoid a recurring plugin is to check if  a plugins depth > 1.  This would stop the plugin from being run if was triggered from any other plugin.  The plugin would only run if triggered from the CRM form. This can resolve the problem of plugins firing more than once but it stops plugins being triggered from other plugins, this might not be the functionality you require.
  • Never save the Organization service in the CRM execution context in a static variable as this will lead to lots of OpenDataReader errors thrown by the CRM platform since we must keep the plugin/custom step stateless as per MSDN plugin best practices.
  • You need to have proper exception handling for your developed plugins to better troubleshoot any unexpected behaviors, for synchronous plug-ins, you can optionally display a custom error message in the error dialog of the web application by having your plug-in throw an InvalidPluginExecutionException exception with the custom message string as the exception Message property value, before throw the exception its good practice to log the exception error, origin and any other kind of helpful logging information in your custom logging location.
  • If you throw InvalidPluginExecutionException and do not provide a custom message, a generic default message is displayed in the error dialog. It is recommended that plug-ins only pass an InvalidPluginExecutionException back to the platform.
  • Plug-ins should exist with others in a project and not be isolated. An example of an exception to this recommendation would be if a plug-in needed to be selectively deployed to an environment, whereas the others are not to be deployed.  There are two areas of impact for this observed pattern of a single plug-in per assembly:
    1. Performance – each plug-in assembly has a lifecycle that is managed by the CRM deployment, which includes loading, caching, and unloading.  Having more than one assembly containing plug-ins causes more work to be done on the server and could affect the time in which it takes for a plug-in to execute.
    2. Maintainability – having more than one project in Visual Studio can make it more difficult to manage.  It also adds additional steps when packaging a solution and managing deployments.

    Consider merging isolated plug-ins into a single Visual Studio project and assembly.

  • Use NOLOCK hint for Microsoft Dynamics CRM QueryExpression and FetchXml requests for CRM entities that are not having frequent changes like configuration for better query execution performance.
  • Update plug-in target entity only contains the updated attributes. However, often the plug-in will require information from other attributes as well. Instead of issuing a retrieve query, the best practice is to push the required data in an image instead by using the Pre-Image.
  • Its advised for logging purposes to use ready made libraries like log4net or Nlog as they are optimized libraries for handling concurrency and high workload scenarios with many logging options and providers.


I will try to keep this post updated with further tips and tricks as soon as I know them.


One thought on “Best Practices when Writing Dynamics CRM Plugins

  1. be careful with the depth check and certain system cascading operations, including depth, already increment depth. it is better to do different checkings prior to issuing a create or update to avoid recursion

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s