Microsoft Dynamics CRM Plugins - Microsoft Dynamics CRM

What is Microsoft Dynamics CRM Plugins?

A plug-in is a custom business logic that integrates with Microsoft Dynamics CRM to adjust or extend the usual conduct of the platform. Plug-ins act as event handlers and are registered to execute on a selected event in CRM. Plugins are written in both C# or VB and can run both in synchronous or asynchronous mode.

Some situations where you would write a plugin are −

  • You need to execute a few business logic including updating certain fields of a record or updating related data, and many others. when you create or replace a CRM file.
  • You need to call an outside web service on positive occasions including saving or updating a document.
  • You need to dynamically calculate the sector values when any file is opened.
  • You need to automate methods including sending e-mails on your customers on sure events in CRM.

Event Framework

The event Processing Framework in CRM methods the synchronous and asynchronous plugin requests through passing it to the event execution pipeline. whenever an event triggers a plugin logic, a message is despatched to the CRM organization net carrier where it may be read or changed through other plugins or any core operations of the platform.

Plugin Pipeline Stages

The complete plugin pipeline is split in multiple levels on which you may register your custom business logic. The pipeline level particular suggests at which level of the plugin execution cycle, your plugin code runs. Out of all of the certain pipeline ranges in the following table, you may register your custom plugins only on Pre- and post-activities. you may’t register plugins on Platform core primary Operations.

Event

Stage Name

Description

Pre-Event

Pre-validation

Stage in the pipeline for plug-ins that are to execute before the main system operation. Plug-ins registered in this stage may execute outside the database transaction.

Pre-Event

Pre-operation

Stage in the pipeline for plug-ins that are to executed before the main system operation. Plugins registered in this stage are executed within the database transaction.

Platform Core Operation

MainOperation

Intransaction,the main operation of the system, such as create, update, delete, and so on. No custom plug-ins can be registered in this stage. For internal use only.

Post-Event

Post-operation

Stage in the pipeline for plug-ins which are to executed after the main operation. Plug-ins registered in this stage are executed within the database transaction.

whenever the CRM utility invokes an event (like saving or updating a file), the following collection of actions takes region −

  • The event triggers a web service name and the execution is exceeded through the occasion pipeline levels (pre-event, platform center operations, post-event).
  • The records is internally packaged as an OrganizationRequest message and finally sent to the inner CRM web provider methods and platform core operations.
  • The OrganizationRequest message is first obtained through pre-event plugins, which can adjust the data before passing it to platform core operations. After the platform middle operations, the message is packaged as OrganizationResponse and passed to the post-operation plugins. The postoperations plugins can optionally adjust this data before passing it to the async plugin.
  • The plugins obtain this records in the form of context item that is passed to the Execute technique after which the further processing happens.
  • After all the plugin processing completes, the execution is passed back to the application which caused the event.

Plugin Messages

Messages are the events on which the plugin (or business logic) is registered. for example, you may sign up a plugin on Create Message of contact entity. this will fire the business logic each time a new touch document is created.

For custom entities, following are the supported messages based on whether the entity is user-owned or company-owned.

Message Name

Ownership Type

Assign

User-owned entities only

Create

User-owned and organization-owned entities

Delete

User-owned and organization-owned entities

GrantAccess

User-owned entities only

ModifyAccess

User-owned entities only

Retrieve

User-owned and organization-owned entities

RetrieveMultiple

User-owned and organization-owned entities

RetrievePrincipalAccess

User-owned entities only

RetrieveSharedPrincipalsAndAccess

User-owned entities only

RevokeAccess

User-owned entities only

SetState

User-owned and organization-owned entities

SetStateDynamicEntity

User-owned and organization-owned entities

Update

User-owned and organization-owned entities

For default out-of-the-box entities, there are extra than 100 supported messages. a number of these messages are applicable for all the entities while a number of them are unique to sure entities. you may locate the entire list of supported message in an excel document in the SDK: SDKMessage-entity support for plug-ins.xlsx

Writing Plugin

In this section, we will research the basics of writing a plugin. we will be developing a pattern plugin that creates a project interest to follow-up with the customer every time a new customer is introduced to the system, i.e. every time a new Contactrecord is created in CRM.

First of all, you will want to consist of the references to Microsoft.Xrm.Sdk namespace. The CRM SDK includes all the required SDK assemblies. Assuming that you have already downloaded and installed the SDK in chapter 2, open visual Studio. Create a new project of type class Library. you may name the project as SamplePlugins and click ok.

Microsoft Dynamics CRM - Plugins

Add the reference of Microsoft.Xrm.Sdk assembly to your project. The assembly is present in SDK/Bin.

Microsoft Dynamics CRM - Plugins

Now, create a class named PostCreateContact.cs and extend the elegance from IPlugin. until now, your code will look something like the following.

Microsoft Dynamics CRM - Plugins

You may also want to add reference to system.Runtime.Serialization. once you have introduced the specified references, copy the following code inside the PostCreateContact class.

Following is a step-through-step explanation of what this code does −

Step 1 − Implements the Execute technique through taking IServiceProvider item as its parameter. The provider issuer includes references to many useful items that you are going to use inside plugin.

Step 2 − Obtains the IPluginExecutionContext item using the GetService technique of IServiceProvider.

Step 3 − gets the goal entity’s item from the context item’s InputParameters series. This Entity class item refers to the contact entity record on which our plugin could be registered.

Step 4 − It then creates an object of project entity and sets proper problem, description, dates, category and regardingobjectid. The regardingobjectid shows for which contact file this activity document is being created. you may see that the code gets the identity of the parent contact document the use of context.OutputParameters and associates it with the project entity record which you have created.

Step 5 − Creates item of IOrganizationServiceFactory the use of the IServiceProvider object.

Step 6 − Creates item of IOrganizationService the use of the IOrganizationServiceFactory object.

Step 7 − finally, using the Create technique of this service item. It creates the follow-up activity which gets saved in CRM.

Signing the Plugin Assembly

This phase is applicable best if you are registering your plugin meeting for the first time. You need to sign in the assembly with a key in order to deploy the plugin. Rightclick the solution and click properties.

Microsoft Dynamics CRM - Plugins

Choose the Signing tab from the left options and check the ‘signal the meeting’ choice. Then, select New from select a strong name key file option.

Microsoft Dynamics CRM - Plugins

Enter the key document name as sampleplugins (this could be any other name you need). Uncheck the protect my key file with a password choice and click ok. click save.

Microsoft Dynamics CRM - Plugins

Finally, build the answer. right click → build. building the solution will generate assembly DLL, which we can use in the next chapter to register this plugin.

Exception Handling in Plugin

More often than not, your plugin logic will want to handle run-time exceptions. For synchronous plugins, you may return an InvalidPluginExecutionException exception, in order to show an errors dialog box to the person. the error conversation will include the custom error message which you pass to the Message item of the exception object.

If you look at our code, we are throwing the InvalidPluginExecutionException exception in our catch block.

Conclusion

Plugins are certainly important to any custom CRM implementation. in this chapter, we focused on information the event framework version, pipeline stages, messages, and writing a sample plugin. in the next chapter, we will register this plugin in CRM and see it working from end-to-end scenario.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Microsoft Dynamics CRM Topics