CRM 2011 Plugin: Generic Plugin Skeleton

using System;

using System.Diagnostics;

using System.Linq;

using System.ServiceModel;

 

/*

       CRM Namespaces

       --------------

       Remember to add the following references from the CRM SDK \bin folder

              Microsoft.Xrm.Client.dll

              Microsoft.Xrm.Sdk.dll

*/

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Query;

using Microsoft.Xrm.Sdk.Messages;

 

namespace CRM_2011_Plugin_Skeleton

{

    public class MyPluginSkleton : IPlugin

    {

        public void Execute(IServiceProvider serviceProvider)

        {

            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

 

            // TODO - If you require tracing, uncomment the following line

            // ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

 

            Entity entity = null;

 

            // Check if the InputParameters property bag contains a target

            // of the current operation and that target is of type DynamicEntity.

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

            {

                // Obtain the target business entity from the input parmameters.

                entity = (Entity)context.InputParameters["Target"];

 

                // TODO Test for an entity type and message supported by your plug-in.

                if (context.PrimaryEntityName != "<entity logical name>") { return; }

                if (context.MessageName != "<message>") { return; }

            }

            else

            {

                return;

            }

 

            try

            {

                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

 

                // TODO - Plugin code goes here

            }

            catch (FaultException<OrganizationServiceFault> ex)

            {

                throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);

            }

        }

    }

}