Summary of CRM 2011 plug-in

Topic 1: IserviceProvider

Defines:

IServiceProvider Interface: Defines a mechanism for retrieving a service object; that is, an object that provides custom support to other objects.

ServiceProvider Contains:

A container for service objects. Contains references to the plug-in execution context (IPluginExecutionContext), tracing service (ITracingService), organization service (IOrganizationServiceFactory), and notification service (IServiceEndpointNotificationService).

Remarks:

Called by the event execution pipeline during processing of a message request for which the plug-in was registered. For more information, see the IServiceProvider Interface. The notification service is only provided for asynchronous registered plug-ins.

How to use:

//Extract the tracing service for use in debugging sandboxed plug-ins.

ITracingService tracingService =

(ITracingService)serviceProvider.GetService(typeof(ITracingService));





// Obtain the execution context from the service provider.

IPluginExecutionContext context =

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



//IOrganizationServiceFactory

IOrganizationServiceFactory serviceFactory =

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



//IServiceEndpointNotificationService

IServiceEndpointNotificationService endPointService =

(IServiceEndpointNotificationService)serviceProvider.GetService(typeof(IServiceEndpointNotificationService));



 

 

Topic 2: ItracingService

 

Function:

Provides a method of logging run-time trace information for plug-ins.

Remarks:

A reference to a tracing service implementation can be obtained from the service provider passed to plug-in’s Execute method.

The ITracingService interface provides a way to log plug-in run-time information. This method of logging information is especially useful for sandboxed plug-ins registered with Microsoft Dynamics CRM Online that cannot otherwise be debugged using a debugger.

The tracing information is displayed in a dialog of the Microsoft Dynamics CRM Web application only if an exception is passed from a plug-in back to the platform

How to use:

//Extract the tracing service for use in debugging sandboxed plug-ins.

ITracingService tracingService =

(ITracingService)serviceProvider.GetService(typeof(ITracingService));

tracingService.Trace("FollowupPlugin begin:");

tracingService.Trace("FollowupPlugin: {0}","Test Tracing Service");

 

How to enable tracing server:

Articles refrences from here: http://support.microsoft.com/kb/907490

How to enable tracing in Microsoft Dynamics CRM

Topic 3: IOrganizationServiceFactory Interface

 

Defines & Functions:

Represents a factory for creating IOrganizationService instances.

Members:

 

Name

Description

                                                                     

CreateOrganizationService

Returns an IOrganizationService   instance for the organization that the specified user is a member of.

 

How to use:

// Obtain the execution context from the service provider.

IPluginExecutionContext context =

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



//IOrganizationServiceFactory

IOrganizationServiceFactory serviceFactory =

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



//IServiceEndpointNotificationService

IServiceEndpointNotificationService endPointService =

(IServiceEndpointNotificationService)serviceProvider.GetService(typeof(IServiceEndpointNotificationService));



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

 

 

Topic 4: IpluginExecutionContext

Function:

Defines the contextual information passed to a plug-in at run-time. Contains information that describes the run-time environment that the plug-in is executing in, information related to the execution pipeline, and entity business information.

Remarks:

The execution context is passed to a plug-in at run time in the System.IServiceProvider parameter of the Execute method. You can obtain the context from the service provider as shown in the following plug-in code.

 

IPluginExecutionContext Members:

 

Name

Description

    

ParentContext

Gets the execution   context from the parent pipeline operation.

    

Stage

Gets the stage in the   execution pipeline that a synchronous plug-in is registered for.

 

How to use:

It contains so many properties,  you can study them by yourself.

 

Topic 5: IServiceEndpointNotificationService Interface

Function:

Posts the plug-in execution context to the Windows Azure platform AppFabric Service Bus.

 

 

Topic 6: How to register a plug-in

 

Plug-in Storage:

Plug-ins not-registered in the sandbox can be stored in the Microsoft Dynamics CRM server's database or the on-disk file system. We strongly recommend that you store your production-ready plug-ins in the Microsoft Dynamics CRM database, instead of on-disk. Plug-ins stored in the database are automatically distributed across multiple Microsoft Dynamics CRM servers in a data center cluster. On-disk storage of plug-ins is useful for debugging plug-ins using Microsoft Visual Studio but is mostly provided for backward compatibility with callouts. You can debug a plug-in that is stored in the database.

Plug-ins registered in the sandbox must be stored in the database regardless of the Microsoft Dynamics CRM deployment (on-premises, IFD/SPLA, or Online).

IExecutionContext.PostEntityImages Property:

Gets the properties of the primary entity after the core platform operation has been completed.

IExecutionContext.PreEntityImages Property:

Gets the properties of the primary entity before the core platform operation has begins.

How to update a plug-in:

  1. End process of w3wp.exe
  2. Disable the services of  sandbox service, asyn service and ..
  3. Use a user as administrator or has the privileges

Topic 7: Sandbox

TOPic 8: Pass data & SharedVariables

Introduce:

The message pipeline model defines a parameter collection of custom data values in the execution context that is passed through the pipeline and shared among registered plug-ins, even from different 3rd party developers. This collection of data can be used by different plug-ins to communicate information between plug-ins and enable chain processing where data processed by one plug-in can be processed by the next plug-in in the sequence and so on. This feature is especially useful in pricing engine scenarios where multiple pricing plug-ins pass data between one another to calculate the total price for a sales order or invoice. Another potential use for this feature is to communicate information between a plug-in registered for a pre-event and a plug-in registered for a post-event.

The name of the parameter that is used for passing information between plug-ins is SharedVariables. This is a collection of key\value pairs. At run time, plug-ins can add, read, or modify properties in the SharedVariables collection. This provides a method of information communication among plug-ins.

How to use & sample code:

/// <summary>
/// A plug-in that sends data to another plug-in through the SharedVariables
/// property of IPluginExecutionContext.
/// </summary>
/// <remarks>Register the PreEventPlugin for a pre-event and the
/// PostEventPlugin plug-in on a post-event.
/// </remarks>
public class PreEventPlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context from the service provider.
Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

// Create or retrieve some data that will be needed by the post event
// plug-in. You could run a query, create an entity, or perform a calculation.
//In this sample, the data to be passed to the post plug-in is
// represented by a GUID.
Guid contact = new Guid("{74882D5C-381A-4863-A5B9-B8604615C2D0}");

// Pass the data to the post event plug-in in an execution context shared
// variable named PrimaryContact.
context.SharedVariables.Add("PrimaryContact", (Object)contact.ToString());
}
}

public class PostEventPlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context from the service provider.
Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

// Obtain the contact from the execution context shared variables.
if (context.SharedVariables.Contains("PrimaryContact"))
{
Guid contact =
new Guid((string)context.SharedVariables["PrimaryContact"]);

// Do something with the contact.
}
}
}

 

posted on 2012-03-27 21:17  Lost In Republic  阅读(457)  评论(0编辑  收藏  举报

导航