Developing a very basic Plug-in for Microsoft Dynamics CRM

Developing a very basic Plug-in for Microsoft Dynamics CRM

In this blog i am going to share how to develop a very basic plug-in for CRM step-by-step. Just follow each and every step and you will create your first Plugin very easily.

1. Download CRM SDK. This will provide you all the information, required SDK assemblies and many helpful samples.
(Download Link for SDK :

2. Set up your plug-in project in Visual Studio. This will be a .NET class library.

3. Add References. At minimum, you will need Microsoft.Xrm.Sdk, obtained from CRM SDK.
4. Extend the class from Microsoft.Xrm.Sdk.IPlugin
5. Write your code (Below a very basic example is given after step 7. write codes in the code area in box shown)

 6. At the project, sign the assembly. This is required in order to be able to deploy the plugin.
 7. Compile the assembly and deploy using Plugin Registration Tool.
(Use this link to know more about how to Use the Plug-in Registration Tool for Microsoft Dynamics CRM and Microsoft Dynamics CRM Online )

So now get ready to create your first Plugin. Here is an example step by step

We are going to create a new plugin to run on the create of an account record In this plugin we are going to create a plugin which is triggered when a new account is created.The plugin will fire after the new account is created so it will be a post plugin message.The plugin will then create a follow up task with a due date in a weeks time.

Plugin code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Crm.Sdk;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Messages;
using System.ServiceModel;

namespace TestPlugIn
    public class Plugin : IPlugin
        /// <summary>
        /// A plug-in that creates a follow-up task activity when a new account is created.
        /// </summary>
        /// <remarks>Register this plug-in on the Create message, account entity,
        /// and asynchronous mode.
        /// </remarks>
        public void Execute(IServiceProvider serviceProvider)
            //Extract the tracing service for use in debugging sandboxed plug-ins.
            ITracingService tracingService =

            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = (IPluginExecutionContext)

            // The InputParameters collection contains all the data passed in the message request.
            if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
                // Obtain the target entity from the input parameters.
                Entity entity = (Entity)context.InputParameters["Target"];

                // Verify that the target entity represents an account.
                // If not, this plug-in was not registered correctly.
                if (entity.LogicalName != "account")

                    // Create a task activity to follow up with the account customer in 7 days.
                    Entity followup = new Entity("task");

                    followup["subject"] = "Send e-mail to the new customer.";
                    followup["description"] =
                        "Follow up with the customer. Check if there are any new issues that need resolution.";
                    followup["scheduledstart"] = DateTime.Now.AddDays(7);
                    followup["scheduledend"] = DateTime.Now.AddDays(7);
                    followup["category"] = context.PrimaryEntityName;

                    // Refer to the account in the task activity.
                    if (context.OutputParameters.Contains("id"))
                        Guid regardingobjectid = new Guid(context.OutputParameters["id"].ToString());
                        string regardingobjectidType = "account";

                        followup["regardingobjectid"] =
                        new EntityReference(regardingobjectidType, regardingobjectid);

                    // Obtain the organization service reference.
                    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                    // Create the task in Microsoft Dynamics CRM.
                    tracingService.Trace("FollowupPlugin: Creating the task activity.");
                catch (FaultException<OrganizationServiceFault> ex)
                    throw new InvalidPluginExecutionException("An error occurred in the FollupupPlugin plug-in.", ex);

                catch (Exception ex)
                    tracingService.Trace("FollowupPlugin: {0}", ex.ToString());

In the code above you can see the class which creates the followup task

Right click on CRMPackage

click build

Click Deploy

Note : While registering your plugin through Plugin resitraion tool, don't forget to Select message as "Create" and pipeline stage to "post-Operation" as we the above code is written for task creation after new acoount is created not before and not with Update or Delete, etc.


Popular posts from this blog

StateCode and StatusCode Values for mostly used entities in Microsoft Dynamics CRM 2013

How to show enlarge image when mouse hover on image or link in Asp.Net(c#) using JavaScript

How to import CSV files into DataTable in C#

go to top image