CRM2011 Step by Step Create and Register Plugin


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.

pen 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"

Following steps give an idea of how we could achieve this in CRM 2011:
(1) Create a new Visual Studio 2010 Class Library Project, on the assumption we are not using a Plugin Template Project.
(2) Add the required references from the CRM 2011 SDK.
Microsoft.CRM.SDK.Proxy
Microsoft.XRM.SDK
(3) Add a final required reference from the .NET Framework
System.ServiceModel
System.Runtime.Serialization




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

?
10
11
12
13
14
15
16
17
18
19
20
21
22
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:

?
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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


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.

Post a Comment

0 Comments