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

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.

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

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

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.