Sample Plugin – MS CRM 4.0

Creating a Simple Plug-in


To complete this walkthrough, you will need the following:

  • Visual Studio 2005 or Visual Studio 2008.
  • A pre-built version of the Plug-in Registration tool.
  • A Microsoft Dynamics CRM SDK installation.
  • Network access to a Microsoft Dynamics CRM 4.0 server.
  • A Microsoft Dynamics CRM system account with either the System Administrator or System Customizer security role, which is also a member of the Deployment Administrators group in Deployment Manager.

You can build the sample directly on the Microsoft Dynamics CRM server assuming all of the prerequisites are met. The Plug-in Registration Tool can be built by following the related walkthrough.

Step 1: Creating a Visual Studio Solution

First you will create a new Visual Studio project and create a Visual C# class file.

To create a Visual Studio solution

  1. In Microsoft Visual Studio, on the File menu, point to New, and then click Project to open the New Project dialog box.
  2. In the Project types pane, select Visual C#.
  3. In the Templates pane, click Class Library.
  4. Type a name for your project and then click OK. Use the name AccountCreatePlugin.
  5. In Solution Explorer, rename the Class1.cs file to Plugin.cs.
  6. When you are prompted to rename all references to this file, click Yes.

Step 2: Adding References

You need to add references to the required Microsoft Dynamics CRM assemblies.

To add the necessary assembly references

  1. In Solution Explorer, right-click the References folder and select Add Reference.
  2. Click the Browse tab and navigate to the location of your Microsoft Dynamics CRM DLLs. This may be in the SDK\Bin folder or on your server in the GAC folder. The GAC folder is located on the Microsoft Dynamics CRM server at <crm-root>\Server\i386\GAC.
  3. Select both Microsoft.Crm.Sdk.dll and Microsoft.Crm.SdkTypeProxy.dll and click OK.
  4. In Solution Explorer, right-click the References folder and select Add Reference.
  5. Click the .NET tab, select System.Web.Services, and click OK.

Step 3: Sign your Plug-in

  1. Right-click the AccountCreatePlugin project in Solution Explorer and click Properties.
  2. Create a new strong key file by clicking the Signing tab, select the Sign the assembly check box and select <New…> in the drop down list.
  3. Type AccountCreateStrongKey and clear the Protect my key file with a password check box. Click OK.
  4. Save your changes by clicking the Save All button.

Step 4: Write your Plug-in Code

This plug-in will create a new task to send an e-mail to the new customer after an account is created. It also shows you an example of throwing plug-in exceptions to the client, which displays an exception message to the user. The complete project source code can be found at SDK\Walkthroughs\Plugin\CS\AccountCreate.

To add required namespaces

  1. Open the Plugin.cs file and add Microsoft.Crm.Sdk and Microsoft.Crm.SdkTypeProxy to the imported namespaces, as shown in the following code example.
  2. Add a semicolon and the word IPlugin after the class declaration.

Your code should now look like this:

using System;
using System.Collections.Generic;
using System.Text;

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

namespace Microsoft.Crm.Sdk.Walkthrough
    public class AccountCreateHandler : IPlugin

To Add the Execute method

The execute method is called when your plug-in is triggered.

  1. Add the Execute method to the CreateAccountHandler class using the following code.

public void Execute(IPluginExecutionContext context)
   DynamicEntity entity = null;

   // Check if the input parameters property bag contains a target
   // of the create operation and that target is of type DynamicEntity.
   if (context.InputParameters.Properties.Contains("Target") &&
      context.InputParameters.Properties["Target"] is DynamicEntity)
      // Obtain the target business entity from the input parmameters.
      entity = (DynamicEntity)context.InputParameters.Properties["Target"];

      // Verify that the entity represents an account.
      if (entity.Name != EntityName.account.ToString()) { return; }

      // Create a task activity to follow up with the account customer in 7 days. 
      DynamicEntity followup = new DynamicEntity();
      followup.Name = EntityName.task.ToString();

      followup.Properties = new PropertyCollection();
      followup.Properties.Add(new StringProperty("subject", "Send e-mail to the new customer."));
      followup.Properties.Add(new StringProperty("description", 
         "Follow up with the customer. Check if there are any new issues that need resolution."));

      followup.Properties.Add(new CrmDateTimeProperty("scheduledstart", 
      followup.Properties.Add(new CrmDateTimeProperty("scheduledend", 

      followup.Properties.Add(new StringProperty("category",

      // Refer to the new account in the task activity.
      if (context.OutputParameters.Properties.Contains("id"))
         Lookup lookup = new Lookup();
         lookup.Value = new Guid(context.OutputParameters.Properties["id"].ToString());
         lookup.type = EntityName.account.ToString();

            new LookupProperty("regardingobjectid", lookup));

      TargetCreateDynamic targetCreate = new TargetCreateDynamic();
      targetCreate.Entity = followup;

      // Create the request object.
      CreateRequest create = new CreateRequest();
      create.Target = targetCreate;

      // Execute the request.
      ICrmService service = context.CreateCrmService(true);
      CreateResponse created = (CreateResponse)service.Execute(create);      
   catch (System.Web.Services.Protocols.SoapException ex)
      throw new InvalidPluginExecutionException(
         "An error occurred in the AccountCreateHandler plug-in.", ex);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s