Understanding Plug-ins in MS CRM
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.
- 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
• 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 EntityCan transform to early bound type using generic methodtargetEntity.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
Reference -
No comments:
Post a Comment