Sunday, January 27, 2008

MSCRM 4.0 Plug-in: Quick Start Guide Part 1 (Creation)

How to create plug-in for MSCRM 4.0:

1. Create a Class Library project.


2. Assign a key to the plug-in. Plug-in assembly must be strong-named and signed with a key. If no existing key available, a new key can be generated:


3. Add following MSCRM 4.0 SDK references:

       microsoft.crm.sdk.dll
       microsoft.crm.sdktypeproxy.dll

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

4. Plug-in must implement the IPlugin interface, for example:

       public class CaseNumber: IPlugin
       {

       }

5. Sample code for generating Case Number (ticketnumber):

public class CaseNumber: IPlugin
{
    public void Execute(IPluginExecutionContext context)
    {
        // Ensure DynamicEntity is available
        if (context.InputParameters.Properties.Contains("Target") &&
            context.InputParameters.Properties["Target"]is DynamicEntity)
        {
            // Retrieve the DynamicEntity
            DynamicEntity entity =
                (DynamicEntity)context.InputParameters.Properties["Target"];
// Ensure the DynamicEntity is incident if (entity.Name == EntityName.incident.ToString()) { // Update the ticketnumber Property entity.Properties["title"]= DateTime.Now.ToString(); } } } }
6. Compile and register the plug-in with the MSCRM Plug-in Registration Tool:


7. The plug-in can be stored (deployed) to MSCRM database, the server hard disk or the GAC. If the Disk option is selected, the assembly file must be placed into C:\Program Files\Microsoft Dynamics CRM\Server\bin\assembly at the server (assuming this is the MSCRM installation path).


8. Register new step for the plug-in:


9. For example, the assembly subscribed to Create event (Message), incident as the Primary Entity, and the Stage is Pre Stage:


10. If Pre-Image or Post-Image is required by the plug-in, a new Image can be registered:


11. Once the plug-in registered, it can be tested by performing relevant actions to trigger the plug-in event.