How to write basic Plugin in Microsoft Dynamics CRM 2011.

A plug-in is custom business logic that you can integrate with Microsoft Dynamics CRM 2011 and Microsoft Dynamics CRM Online to modify or augment the standard behavior of the platform. Plug-ins are event handlers since they are registered to execute in response to a particular event being fired by the platform.

Plug-ins are custom classes that implement the IPlugin interface. You can write a plug-in in any .NET Framework 4 CLR-compliant language such as Microsoft Visual C# and Microsoft Visual Basic .NET. To be able to compile plug-in code, you must add Microsoft.Xrm.Sdk.dll and Microsoft.Crm.Sdk.Proxy.dll assembly references to your project. These assemblies can be found in the SDK\Bin folder of the SDK download.

using System;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;

namespace Microsoft.Crm.Sdk.Samples
{
    public class SamplePlugin: IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = 
                (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            // Get a reference to the Organization service.
            IOrganizationServiceFactory factory = 
                (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);
            try
            {
                // Plug-in business logic goes below this line.
                // Invoke organization service methods.
            }
            catch (FaultException<OrganizationServiceFault> ex)
            {
                // Handle the exception.
            }
        }
    }

Each plug-in assembly must be signed, either by using the Signing tab of the project’s properties sheet in Microsoft Visual Studio 2010 or the Strong Name tool, before being registered and deployed to Microsoft DynamicsCRM. For more information about the Strong Name tool, run the sn.exe program, without any arguments, from a Visual Studio 2010Command window.

This entry was posted in MS CRM 2011 and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *