Using the SerializePluginContext Solution to test CRM 2011 Plugins 


Acknowledgements and Licensing

This project is a port of the MSCRM4PluginTesting solution posted on by Dinesh Uthayakumar, released under the GPLv3. Major thanks to him for thinking up the general approach and giving me a framework to work off of. Carlton Colter of Microsoft also contributed to this project by streamlining the code and adding support for the parent context property.

This code is released under the GPLv3 as well.


This solution is meant to make unit testing of plugins in CRM 2011 a simpler and more efficient process. One of the challenges of testing plugins is that they usually must be registered to the CRM server before testing. This is a fairly lengthy process, making quick testing of changes, and well as automated unit testing, and difficult process.

This solution addresses that issue by serializing the objects that the CRM server passes to a plugin on execution and then offering a library that allows you to deserialize those objects and pass them to the plugin on your local machine. This way you can simulate actually having the plugin registered against the server but without having to go through the process of registering it for each and every change.


The solution has two projects.

  1. SerializePluginContext – This a the CRM 2011 plugin that handles serializing the IPluginExecutionContext object, used by all plugins to get information about what message, entity, etc that the plugin is running under
  2. TestPlugin – This is a .NET class that handles deserializing the IPluginExecutionContext object created by SerializePluginContext, and then constructing an implementation of the IServiceProvider object with connections to the CRM server to pass to your plugin’s Execute method

How To Use

Serializing the Plugin Context

  1. Compile and register the SerializePluginContext plugin on the CRM server using the plugin registration tool.(Note if you downloaded the release instead of the source code, this will already be compiled). When registering the plugin, register it exactly as if you were registering the plugin you are testing, including setting up appropriate Pre-Images and Post-Images. If you want to test multiple scenario’s register it for multiple steps.
  2. Manually take the appropriate steps (i.e., creating records, updating records, etc.) to fire off the plugin. Validate that it functioned without error by checking the system jobs entity for a successful record of plugin execution
  3. Go to C:\windows\temp\ and look for a file with a named [Primary Entity] –[ Message], where primary entity is the primary entity you registered the plugin against, and the message is the CRM 2011 plugin message
  4. Copy this file back to your local development machine and into your solution folders.

Using the Serialized Plugin Context in Your Tests

  1. Add the TestPlugin.dll class as a reference to your testing project.
  2. To use the serialized plugin context create a new instance of the MyServiceProvider object. There are four constructors for MyServiceProvider for different scenarios 
  • MyServiceProvider (string filepath)

For when your tests don’t require connecting to the CRM service. This will create a new MyServiceProvider object with the MyServiceProvider.context property set to the deserlized IPluginExecutionContext Object found at “filepath”

  • MyServiceProvider (string username, string password, string servername)

For when your tests don’t require the plugin context, but do require connecting to a CRM Server

  • MyServiceProvider(string username, string password, string servername, string filepath)

For when your tests require both the context and a connection to the server.

Once you’ve constructed the MyServiceProvider object you can either pass the object to your Execute method directly, or get access to the context and crm service connections using the same code you would in working with the IServiceProvider object.

Last edited Nov 19, 2010 at 9:05 PM by AndrewSwerlick, version 7


Code_Researcher Jan 17, 2013 at 1:39 PM 
The file is saved on the CRM server not on your local machine!

diegoeliassantos Nov 24, 2012 at 4:23 AM 
I'm not finding the files in temp folder. Where more can I look for?

Go to C:\windows\temp\ and look for a file with a named [Primary Entity] –[ Message], where primary entity is the primary entity you registered the plugin against, and the message is the CRM 2011 plugin message