Dynamics CRM Plugin

CRM Plugins

Mastering CRM Plugins: Customizing Dynamics 365 with .NET C#

Introduction and Concept to Plugin

  • Plugins are custom business logic or code which modifies the default behavior of CRM platform.
  • This requires .net c# programming concept and a developer knowledge with CRM SDK knowledge.
  • As a technical term Plugin is a .net class library(.dll) which uses the reference of Microsoft.Xrm.Sdk.dlland Microsoft.Crm.Sdk.Proxyassembly to interact with CRM Organisation to achieve the task. As plugin interacts with CRM service so System.Runtime.Serialization namespace is required.
  • Plugin are also called as CRM Event Handlers which reacts on events raised by CRM platform. Events are like Create Record, Update Record, Retrieve Record, Delete Record etc..

Plugin Pipeline Stages

  1. Pre-Validation:- security checks being performed to verify the calling or logged on user has the correct permissions to perform the intended operation.
  2. 2. Pre-Operation:- execute before the main system operation. Plug-ins registered in this stage are executed within the database transaction.
  3. Post-Operation :- plug-ins which are to execute after the main operation. Plug-ins registered in this stage are executed within the database transaction.
  1. Start Visual Studio.
  2. Select File > New > Project.
  3. Select Class Library (.NET Framework) and select Next.

4) Select the Browse tab, search for and select microsoft.crmsdk.coreassemblies, and then select Install.

5) Add the using statements to the new class as follows:

Code:-
using Microsoft.Xrm.Sdk;
using System.Text.RegularExpressions;

6) Implement the interface member.

Task 2: Format a phone number

Code:-

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

2) Check the input parameter for Target. Add the following snippet to the Execute method.

Code:-

if (!context.InputParameters.ContainsKey(“Target”))
throw new InvalidPluginExecutionException(“No target found”);

3) Add the following snippet to the Execute method. This snippet will get the target entity from the input parameter and then check if its attributes contain telephone1 (Business Phone for Contacts, Phone for Accounts).

Code:-
var entity = context.InputParameters[“Target”] as Entity;
if (!entity.Attributes.Contains(“telephone1”))
return;

4) Add the following snippet to the Execute function. This snippet will remove all nonnumeric characters from the user-provided phone number.

Code:-
string phoneNumber = (string)entity[“telephone1″];
var formattedNumber = Regex.Replace(phoneNumber, @”[^\d]”, “”);

5)Set telephone1 to the formatted phone number. Add the following snippet to the Execute method.

Code:-
entity[“telephone1”] = formattedNumber;

Topic 3: Register a plug-in and steps

1) Start the plug-in registration tool that is in your Dynamics 365 SDK folder.

 Note:-
If you don’t have the Dynamics 365 SDK tools, see Download tools from NuGet to download.

Exercise 2: Create/Retrieve plug-ins

you will create a plug-in that will run on retrieve. This plug-in will add parentheses and dashes to the phone numbers.

Task 1: Create a plug-in

1) Start Visual Studio.
2) Open the project that you created in Exercise 1.
3) Right-click the project and select Add > Class.

4) Add the using statements to the new class as follows:

using Microsoft.Xrm.Sdk;

Task 2: Update plug-in assembly and register steps and Test

Interested? Let's get in touch!

Get your project started with India's top 10% IT experts
Hire Now!