HOW TO: Create simple plugin in MS CRM 2011

2
Today I will describe how to create a simple plugin and register it in MS CRM 2011. Plugin will create a task each time user create new lead.
To do that I need: Visual Studio 2010 and .NET Framework 4.0 installed on my dev. machine, Microsoft CRM SDK.

1. Open Visual Studio and create new “Class Library” project.

2. Add new Reference

a. Press Add New Reference


b. Navigate to folder where you extracted Microsoft CRM SDK and select file
bin\microsoft.xrm.sdk.dll and press "Ok"



3. Rename Class1.cs to SimplePlugin.cs and change as shown below

using System;
using Microsoft.Xrm.Sdk;

namespace HowTo.SimplePlugin
{
    public class SimplePlugin : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            throw new NotImplementedException();
        }
    }
}

4. Implement business logic

Below you can find code which implement business logic (creates a task each time we have new lead in the system). Modify Execute method as shown:

public void Execute(IServiceProvider serviceProvider)
{
 Entity targetEntityImage = null;

 var pluginExecutionContext =
  (IPluginExecutionContext)serviceProvider.GetService(
  typeof(IPluginExecutionContext));

 if(pluginExecutionContext.PrimaryEntityName != "lead" &&   
 pluginExecutionContext.MessageName != "Create")
 {
  throw new Exception(
   "This plugin should be fired on create of lead only.");
 }

 if (pluginExecutionContext.InputParameters.Contains("Target") &&
  pluginExecutionContext.InputParameters["Target"] is Entity)
 {
  targetEntityImage = 
   (Entity)pluginExecutionContext.InputParameters["Target"];
 }
 else
 {
  throw new Exception("No Target Entity Image found");
 }

 try
 {
  var factory =
  (IOrganizationServiceFactory)serviceProvider.GetService(
   typeof(IOrganizationServiceFactory));

  var service =
   factory.CreateOrganizationService(pluginExecutionContext.UserId);

  Entity task = new Entity("task");

  task.Attributes.Add("subject", 
   "New lead should be qulified in 2 weeks");
  task.Attributes.Add("scheduledstart", DateTime.Now.AddDays(14));
  task.Attributes.Add("scheduledend", DateTime.Now.AddDays(14));
  task.Attributes.Add("regardingobjectid", 
   targetEntityImage.ToEntityReference());

  service.Create(task);

 }
 catch (Exception ex)
 {
  throw new InvalidPluginExecutionException(
     "An error occurred in the Lead on Create plug-in.", ex);
 }
}

5. Enable assembly signing

a. Go to the project properties


b. Go to the Signing tab and select <New…>


c. Inter file name and press Ok


6. Build the project

Now we are ready to build the project. Let’s do that as shown below. Ensure that build was successful.


7. Register plugin

a. Open Plugin Registration tool. Press “Create New Connection”, fill “Label”, “Discovery Url” (Your crm server url. In my case it was http://mscrm), “User Name” and press “Connect”.

Plugin registration tool is provided as separated tool. The source code of Plugin registration tool can be found in Microsoft CRM SDK. You have to build this tool once for future usage.


b. Double click organization name you are going register plugin for. (In my case it was plainCRM). New tab will appear. Then press register new assembly.


c. Select dll you get on step 6. If everything ok press “Register Selected Plugins”. If process successfully finished system will show you popup messages “The selected Plugins have been registered”.


d. Last thing we need to do is to register a plugin step. To do that select plugin you’ve just registered and press register new step.


e. Fill form as shown below and press “Register New Step”


8. Go to Crm and create new lead. Check if new task was created by the plugin.

You can download Visual Studio project here:

2 comments:

Anil Chelasani said...

Nice post in detail. Let's give information on Unit Testing of plugin also

http://exploringxrm.wordpress.com/2012/01/23/unit-testing-crm-2011-plugin/

Henk Herkelman said...

Hello at point 7c I get an error

"Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Action failed for assembly 'HowTo.SimplePlugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=499e62812881acad': Assembly must be registered in isolation."

What do I have to do?
Kind regards

Post a Comment