Introduction to Plug-in Development for Microsoft Dynamics CRM 4.0

Microsoft Dynamics CRM 4.0 on-premise supports extending the platform through the integration of custom business logic known as plug-ins. In Microsoft Dynamics CRM 3.0, these business logic extensions are known as callouts. Plug-ins execute in a richer run-time environment than before and have access to new product capabilities. This article provides an overview of the new plug-in capability, compares plug-ins to callouts, and discusses how to begin learning the new plug-in programming model.

Applies To

Microsoft Dynamics CRM 4.0

Microsoft Visual Studio 2005

Microsoft Visual Studio 2008

Introduction

One method of customizing or extending the functionality of the Microsoft Dynamics CRM 4.0 on-premise product is through the integration of custom business logic (code). It is through this extension capability that you can add new data processing features to the product or alter the way business data is processed by the system. You can also define the specific conditions under which the custom business logic is to execute. Whether you are new to extending Microsoft Dynamics CRM or have been developing 3.0 callouts for some time, this article tells you what you need to know to get started learning about and writing plug-ins.

Microsoft Dynamics CRM Online does not support plug-ins. However, you can extend the functionality of the product by using workflows.

Comparing Plug-ins to Callouts

The programming model for adding business logic extensions to Microsoft Dynamics CRM has changed in the latest Microsoft Dynamics CRM 4.0 SDK release as compared to the 3.0 release. This change was the direct result of customers asking for access to more capabilities and run-time information in plug-in code. In addition, architectural changes and feature additions to Microsoft Dynamics CRM 4.0 necessitated changes to the programming model so that plug-ins could take advantage of the new platform capabilities.

What about your existing callouts? Do you have to throw them away and develop new plug-ins? The good news is that Microsoft Dynamics CRM 4.0 is backwards compatible with the callout programming model. Your existing callouts should continue to work alongside any new plug-ins that you develop as long as you do not use any deprecated features. However, if you want to take advantage of the new Microsoft Dynamics CRM 4.0 capabilities and the rich information that is available at run time, you need to make use of the plug-in programming model.

The following points highlight what has changed when comparing the new plug-in programming model to the previous callout model.

  • Registration
    Callouts are registered by editing an XML configuration file that is stored in a specific folder on the Microsoft Dynamics CRM 3.0 server. In essence this is a static registration method. Changes to the configuration file require an IIS reset to apply the changes.
    Plug-ins are registered dynamically through a new registration API. No IIS reset is required. Sample tools to register plug-ins, complete with source code, are provided in the SDK.
  • Context
    Callouts received a basic amount of data at run-time about the user who initiated an operation in Microsoft Dynamics CRM and the entity being acted upon.
    Plug-ins receive a wealth of information at run-time. For more information, see the following What’s New topic.
  • Supported messages
    Callouts could only be executed in response to a subset of messages that were processed by the Microsoft Dynamics CRM platform.
    Plug-ins can execute in response to most messages being processed by the platform.
  • Mode of execution
    Callouts were executed synchronously as part of the main execution thread of the platform. Callouts that performed a lot of processing could reduce overall system performance.
    Plug-ins can execute both synchronously and asynchronously. Asynchronous registered plug-ins are queued to execute at a later time and can incorporate process-intensive operations.

What’s New

In addition to the plug-in features mentioned in the previous topic, the following capabilities are also supported.

  • Infinite loop detection and prevention
    The Microsoft Dynamics CRM platform has the ability to terminate a plug-in that performs an operation that causes the plug-in to be executed repeatedly, resulting in a significant performance hit on the system.
  • Plug-ins receive expanded run-time information (context)
    Information passed to plug-ins include: custom data, the conditions under which the plug-in was run, information included in the request and response messages that the system is processing, and snapshots of entity attributes before and after the core system operation. Plug-ins can also pass data between themselves.
  • Execution dependency
    Plug-ins can be registered so as to be dependent with other plug-ins. Dependency defines an order to plug-in execution whereby one plug-in must run to completion before another plug-in executes.
  • Database deployment
    Plug-ins can be deployed to the Microsoft Dynamics CRM database in addition to on-disk and GAC deployment. Deploying a plug-in to the database enables automatic distribution of the plug-in to multiple Microsoft Dynamics CRM servers in a data center.
  • Offline execution
    Plug-ins can be deployed to Microsoft Dynamics CRM for Microsoft Office Outlook with Offline Access and execute while Outlook is in offline mode.

A Sample Plug-in

So you now know about the powerful plug-in capabilities and the extensive data passed to a plug-in at run-time. But what does plug-in code look like? Here is a very basic plug-in that displays "Hello world!" in a dialog to the user.

using System;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;

namespace MyPlugins
{
public class HelloWorldPlugin: IPlugin
{
public void Execute(IPluginExecutionContext context)
{
// Call the Microsoft Dynamics CRM Web services here or perform
// some other useful work.
throw new InvalidPluginExecutionException("Hello world!");
}
}
}

The real power of plug-ins lies in the extensive context information that is passed to the plug-in, the ability to alter some of that information as it passes through the system, and the ability to call Microsoft Dynamics CRM Web methods. For more information, refer to the SDK documentation.