Monday, December 15, 2008

MS CRM 4.0 Plug-ins vs. CRM 3.0 Callouts

Plug-ins are a huge improvement over the callouts available in MS CRM 3.0. They are much more flexible, much easier to deploy, and have a many more triggers allowing a lot more access to the inner operation of your CRM system.

The following comparison shows some of the key differences:

Callout

Plug-in

Deployment

Requires Workflow service to be restarted to re-read the callout.config.xml which defines which assemblies and classes to call for each triggered event.

Requires assemblies be copied to each CRM server in your web server farm. Because those assemblies can be busy this can require:

  1. Draining each server in an NLB configuration.
  2. An IIS reset
  3. Stopping the Workflow service.
  • Allows deployment to the database in a single step for all CRM servers.
  • Or deployment to the GAC on each server.
  • Or deployment to a folder for debugging.

 

  • Requires a strongly typed assembly.

Flexibility

Requires a different interface for each Message. A PreCreate has a different interface from a PostCreate, or a PreUpdate, or a PostUpdate. Has the same interface for all messages.

Supports many more messages
Supported Messages for Plug-ins

  Has parent and child pipeline feature
  Has the ability to watch for endless loops.

Coding

Requires parsing Xml images to see the contents of the entity generating the message: preImageEntityXml, postImageEntityXml

Returns a Dynamic Entity or Moniker instead.

var entity = (DynamicEntity)context.InputParameters.Properties[ParameterName.Target];

var myMoniker = (Moniker)context.InputParameters.Properties[ParameterName.Target];

Allows validity checking and stopping a change to the database in a preCallout

errorMessage = "Validation Failed."; return PreCalloutReturnValue.Abort;
                

Allows validity checking and stopping an operation in a Pre message.

throw new InvalidPluginExecutionException(
"Validity Failed. {1}");

Allows modifying data before it is written to the database in a  preCallout.

Parse and Modify the entityXML               

Allows modifying data before it is written to the database in a Pre message.

Modify the Dynamic entity.

String nameProperty = entity.Properties["name"] as String;
nameProperty = "new name";


Resources

VS Plug-in template This template will create a basic Plug-in Project and is a good starting point.

  • The example it creates is for a Dynamic Entity Target. This is useful for Create and Update messages, but many other messages will return a Moniker which gives you the id of the calling entity.
  • The template includes code to call a customized crmservice which can be useful, although it is faster and a better practice to call your service from the context as shown below if you are comfortable with Dynamic Entities:

    ICrmService service = context.CreateCrmService(true);

Plugin Deployment Tool This tool will allow you a simple way to deploy your plug-ins. and has an easy to use UI.

With plug-ins it is important to learn about Dynamic Entities.

If you purchase David Yak's CRM as a Rapid Development Platform, it comes with a number of helper classes for Dynamic Entities, and plug-ins as well as other useful tools that are interesting. His chapters on plug-ins and dynamic entities are useful, and the code he provides is worth downloading and referring to. He also has chapters specific to using his plug-in framework for debugging and other tasks which are not CRM development generic.

I recently ordered a copy of Programming Microsoft Dynamics CRM 4.0, and I'll try to remember to update this post after I have looked over its coverage of Plug-ins and dynamic entities.

No comments: