Basic Plug-in Plugin — MS CRM

Writing a Basic Plug-in

The following sample shows some of the common code found in a plug-in. For this sample, the code omits any custom business logic that would perform the intended task of the plug-in. However, the code does show a plug-in class that implements the IPlugin interface and the required Execute method.


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 = 

            // Get a reference to the organization service.
            IOrganizationServiceFactory factory = 
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);

            // Get a reference to the tracing service.
            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

                // Plug-in business logic goes below this line.
                // Invoke organization service methods.
            catch (FaultException<OrganizationServiceFault> ex)
                // Handle the exception.


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