Saturday, May 21, 2011

Understanding Plug-ins in MS CRM 2011

What is a Plug-in??

A plug-in is custom business logic (code) that you can integrate with Microsoft Dynamics CRM 2011 and Microsoft Dynamics CRM Online to modify or augment the standard behavior of the platform. Another way to think about plug-ins is that they are handlers for events fired by the Microsoft Dynamics CRM platform. You can subscribe, also known as registering, a plug-in to a known set of events to have your code run when the event occurs.

CRM Event Pipeline

  • Calls to the CRM web services execute some message.
  • Each message creates a pipeline made up of various stages.
  • Plug-ins can be registered on different stages in the pipeline
  • A CRM “2011” plug-in is a .NET class that implementsMicrosoft.Xrm.Sdk.IPlugin
  • Assemblies registered with CRM and stored in database, GAC or on disk

CRM Event Pipeline Visualization

PreEvent

Stage 10: Pre-validation

PreEvent

Stage 20: Pre-operation

Core Operation

Stage 30: MainOperation

PostEvent

Stage 40: Post-operation

PostEvent

Stage 50: Post-operation (deprecated*)

Note: 20,30,40 are Transaction Scope


Parent/Child Pipelines

  • Parent and Child Pipelines no longer exist
  • Pre-event Plugin in Parent Pipeline in CRM 4.0

Pre-validation in CRM 2011 (outside transaction)

  • Pre-event Plugin in Child Pipeline in CRM 4.0

Pre-operation in CRM 2011 (inside transaction)

Sync/Async Execution

  • Plugins can execute

- Synchronously during pipeline execution

- Asynchronously – queued for later execution

Plug-ins in CRM4

  • Plug-ins ran in the same process context and not isolated
  • Plug-ins could not participate in SQL transactions
  • Plug-in registration had to be done by a Deployment Administrator

Plug-ins in CRM 2011

  • Able to participate in SQL transactions
  • Able to create traces returned with exceptions
  • Plug-in assemblies can have 2 isolation modes:

- None or Sandbox

  • The addition of the Sandbox isolation mode enables the use of plug-ins in CRM Online.

- Custom workflow activities will not be enabled in CRM Online for CRM 2011

Writing a Plugin

  • Implement IPlugin

Just the Execute method

  • Constructor can take two strings

Unsecure config

Secure config

  • Work with data from IPluginExecutionContext

Target, pre-images and post-images etc…

Handed a late bound Entity
Can transform to early bound type using generic method
targetEntity.ToEntity()

  • Ensure plugin code is stateless

Do not assume state is maintained between executions

Pre/Post Event Images

  • Snapshot of Entity state at that point in time
  • Removes need to call RetrieveRequest to get entity state during execution
  • Attributes to be imaged must be specified in registration

- SdkMessageProcessingStepRegistration.Images

  • Available via properties at runtime

- Pre: IPluginExecutionContext.PreEntityImages

- Post: IPluginExecutionContext.PostEntityImages

Offline Support

  • Offline plugins execute in context of CRM Outlook Add-in
  • Plugins can be: Online/Offline/Both
  • Check if offline via
    %ExecutionContext%.IsExecutingOffline
  • Offline plugins should be idempotent

- May be executed twice

- Use IsOfflinePlayback property to check

Transaction Support

  • CRM 2011 Stages support plugin execution inside the database transaction

- Stages 20 & 40

  • Uncaught exceptions force a rollback
  • IExecutionContext.IsInTransaction
  • Transaction spans CRM DB operations only

- No distributed transaction support

  • Plugins registered outside transaction stage will participate in transaction if executed as nested pipeline of transactional parent

8 comments:

  1. Great explanation, thanks for sharing.

    ReplyDelete
  2. EXECELLENT MADHUMUTHUSAMY
    regards
    william

    ReplyDelete
  3. excellent post.Thank you for the detailed explanation.I have a request though, if you can provide basic samples or scenarios for all the stages as well as secure and unsecure would be much appreciated. If you already have the samples can you post them to me .keep up the good work.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Great explanation!!
    I have a question here. I have created a entity(say entity1) records using XrmServiceContext using early binding proposedAddendumId.

    proposedAddendumId = objProcessLogic.CreateProposedAddendum(process.real_BrandId, process.real_applicationId, process.real_CompanyId, CaseName, workTypePackId, dealType, process.real_name, ServiceContext);
    //Create SubProcess For Process
    objProcessLogic.CreateSubProcessForProcess(process.real_WorkTypeId, process.real_applicationId, process.real_processId, salesDealId, proposedAddendumId, ServiceContext, CrmOrgService, Context,Factory);

    In second method, I am using executemultiplerequest concept. It is telling that proposedAddendumId does not exist in DB. I checked DB,no record with this id. But , in plugin on the fly, I am getting proposedAddendumId.

    Any help will be appreciated.

    ReplyDelete