Create Plug-in of Microsoft Dynamic CRM 2013 in C#.NET

For the scope of this article, I assume that you are familiar with C# OOP concepts and C# projects.

  • We will create a plug-in in the C# Class library, you can also use a portable class library.

  • Add a class library project into the C# solution and name your class "MyDynamicPlugin".



  • Now add references to the 2 DLLs.

    • Microsoft.Crm.Sdk.Proxy.dll
    • Microsoft.Xrm.Sdk.dll

  • And you will find both of the DLLs in the SDK/bin folder of your Microsoft SDK.

  • And inherit the IPlugin interface.

    create IPlugin

  • Implement the IPlugin Interface and you will get an Execute method with the IserviceProvider interface object as a parameter.

  • Execute method: This method runs as an entry point for any plugin in Microsoft Dynamic CRM.

  • IserviceProvider interface: This interface provides access to various services of dynamic, this interface has a method called GetService() that uses the reflection feature of .NET and allows us to get any type of service we want.

    cs code
  • This Execute method is all the code you need to fire before or after any event occurred.

Let's see how

For this article I will create a default Contact whenever anyone creates an account.

For example, If an employee of an organization creates an account then a default contact with basic details should be added to that account automatically (using a plug-In).

Procedure

Step 1

Since we need to trigger our plug-in when an event is executed, we need to get the service of IPluginExecutionContext using the IServiceProvider object.

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

And the GetService method returns an object of the specified type. We need to type caste that object.

Step 2

Plugin execution is an object with a property called InputParameter. 

  • Using this Inputparamter we can get the entity and other types of dynamic CRM.

  • Target is one of the main parameters of this IPluginExecutionContext object.

  • All the plug-in events are first stored in this variable called “Target”.

  • So when any events occur we need to check if this variable is there in our current context execution.

  • And if so then we need to check if it's an entity. Because for this article we will trigger a plug-in if a new account (Entity) is created.

Entity

Step 3

Now when you know, we have a value in the target and it's an Entity.

We can typecast it into an Entity and using its logicalname property we can determine if the current executing event is for an account Entity or not.

And if it's an account entity we can write our creation of contact code here and associate that contact with the currently executing account.

executing account

Step 4: The last step is to create an Organizationservicefactory instance and we need to create a new plug-in, add a pluginexecutioncontext object ID to create an Organization service and based on that service client we can create our custom Event.

  1. IOrganizationServiceFactory servicefactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));  
  2. IOrganizationService client = servicefactory.CreateOrganizationService(context.UserId);   
  3. client.Create(createcontact);  
Your Microsoft Dynamic CRM plug-in is created.

This is the sample code, you can find the attachment of this code with my article.

code

Build your Project.

Important Step

Once you build your project, you need to generate a key that will act as certificate for your plug-in. Without this key you cannot deploy a plug-in onto a server.

To create a key, go to project property (ALT+ Enter) and go to the signing tab and check sign the assembly.

Select new in the drop down and add a key name and your dynamic CRM Password. And select save (CTRL+ S).

If you have done this then it will add a pfx encrypted key for your current project.

Rebuild your project

Procedure to deploy this assembly using SDk\Tools\PluginRegistration\PluginRegistration.exe.

Step 1 
  • Run it and Click Create New connection,
  • Enter your Login Credentials for Connection with Dynamic CRM.
  • The first time you login, you need to pass all the details of your CRM Account.

Step 2

After logging in successfully, click Register > Register New Assembly.

Register New Assembly

Browse to the DLL you just created and click OK.

Step 3

  • Now we need to add steps of this DLL execution.
  • So select the assembly and again click Register > Register new Steps.
  • Or just right-click the assembly you just added and select Register new Steps.

    Register New

This Window includes an option for how and when you want to execute or fire your assembly or plug-in.

Message: It defines on which event you want to run the plug-in.

Example: Create.

Primary Entity: It define on which Entity execution you want to run the plug-in.

Example: account.

Event pipeline stage of Execution: It defines when you want to run the DLL, before the core operations or after the core operations.

Execution Mode:
Execution can be synchronous by default or can be asynchronous (that will be handled further by the Dynamic CRM Queue manager).

Deployment: Server where every plug-in is deployed.

Offline where you can deploy it on Outlook for offline use.

Click Register New Step and boom, it's done.

Your assembly or plug-in is deployed on the server. Now go to Dynamic CRM and create an account, an automated contact will be added by default in that account without any manual entry from employees.