Pogo69's Blog

April 19, 2011

Debugging Plugins in CRM Online

Filed under: C#, CRM — Tags: , , , , — pogo69 [Pat Janes] @ 16:10

Plugin development in CRM Online offers a unique challenge, in that we cannot interactively debug our code.

In the ideal world, you would test your plugin in a development/testing environment (VPC or equivalent) before moving your code to the production Online deployment.  However, if you either have no testing environment available or are experiencing issues specific to the production deployment (data dependent errors, for instance), a few lines of code can help ease the pain:

	public class Plugin : IPlugin
		public void Execute(IServiceProvider serviceProvider)
			IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

			// TODO - If you require tracing, uncomment the following line
			ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

			Entity entity = null;

			// Check if the InputParameters property bag contains a target
			// of the current operation and that target is of type DynamicEntity.
			if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
				// Obtain the target business entity from the input parmameters.
				entity = (Entity)context.InputParameters["Target"];

				// TODO Test for an entity type and message supported by your plug-in.
				if (context.PrimaryEntityName != "<logicalentityname>") { return; }
				if (context.MessageName != "<message>") { return; }
				IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
				IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

			catch (FaultException<OrganizationServiceFault> ex)
				System.Text.StringBuilder errors = ex.Detail.ErrorDetails.Aggregate(
					new System.Text.StringBuilder(), (list, error) =>
						list.Append(string.Format("[{0}:{1}]", error.Key, error.Value));
						return list;

				#if TRACE == true
					@"ErrorCode: {0}
					ErrorDetails: {1}
					Message: {2}
					TraceText: {3}",

				throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);

With your exception handler in place, when/if an exception is thrown, the details of the Exception will be written to the Trace Log which is available for download via the popup Error dialog.  Anything written to the Tracing service will be sent to the CRM Web Application client in this way, but only if the plugin throws an exception.



  1. Looks like you are an expert in this field, excellent article and keep up the great work, my buddy recommended me your blog.

    My blog:
    rachat credit marseille aussi Rachat de Credit meilleur taux

    Comment by Adrien — May 4, 2011 @ 17:37

  2. Nice Article dude.. I am new to Online CRM 2011 version. Your article gives me an overview of debugging plugins..

    Thanks & keep up the good work…

    Comment by Guru Prasad — November 4, 2011 @ 16:20

  3. i would like to know if you have a sample about plugin for crm online. Sorry i dont’ speak so much english.

    Comment by eespinoza — January 27, 2012 @ 14:41

RSS feed for comments on this post. TrackBack URI

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Blog at WordPress.com.

%d bloggers like this: