Monday, 15 April 2019



Hi guys , 

I would like to provide brief details regarding how to Implement Plugin in MS CRM which is Beginner and Intermediate level. 

MS CRM Version : 9.0 ( Online) 

Topics Highlighted are :

  1. How to get Dll from Nuget
  2. How to get Plugin Registration Tool and Configuration Migration tool
  3. Sample Plugin code
  4. Difference between Isolation Mode None vs Isolation

Article : 


How to write a Plugin

1.       Go to this website and copy the script :

2.       Copy the Script mentioned in website as shown below :




3.       Open Windows PowerShell in Windows machine and run the script which we got from above

4.       It will download Tools folder from this link which contains Sdk dlls and Plugin Registration tool.



 Note : SDK dlls can be downloaded from Nuget package manager as well , but Plugin Registration Tool and Configuration Manager Tool is available  only via  running Powershell Script as mentioned in step 1.

5.       SDK Dll version compatibility – Microsoft recommends sdk dll version and CRM version should match. For example – if my org is on version (8.2) SDK dll should be on version ( 8+). This is cause there are few Architectural changes in different CRM versions. Hence few functionalities will not be compatible if we use old dlls.

6.       Open Visual Studio and select Class Library ( .Net Framework) to create a new plugin assembly

7.       Include Refence dll i.e. like Microsoft.xrm.sdk.dll in your class library project as shown below







8.       Sample CRM Plugin code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Crm.Sdk;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Tooling;

namespace Plugin_Library
{
    public class UpdatePluginTest : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            // Get Plugin Execution Context

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

            // Get Organization Service from Organization Service Factory

            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

            IOrganizationService service = serviceFactory.CreateOrganizationService(executionContext.UserId);

            if(executionContext.InputParameters!=null)
            {

                try
                {
                    // For creation of Record

                    Entity entity = new Entity("Account");

                    entity["name"] = "JK Test";

                    service.Create(entity);
                }
                catch(Exception e)
                {
                    throw new Exception(" Error in creation of record. Error Message is " + e);
                }

                // For update of Record

                try
                {

                    Entity entity = new Entity("Account");

                    EntityReference entityReference = (EntityReference)entity.Attributes["name"];

                    Entity parentEntity = new Entity("Account");

                    parentEntity.Id = entityReference.Id;

                    parentEntity["name"] = " JK";


                    service.Update(parentEntity);
                }
                catch(Exception e)
                {
                    throw new Exception(" Error in update operation and the error message is" + e);
                }

            }

            throw new NotImplementedException();
        }
    }
}

Here Iplugin is the Interface which is present in SDK base class and it contains Execute method which is the opening point for execution to start.

Ø  IPlugin Execution Context brings in Plugin details like execution pipeline , message on which plugin triggers , user context , assembly location.
Ø  IOrganizationServiceFactory brings in all data\component information in the org in order to perform operations on the data.

Ø  Once the plugin is built – Build The Solution to acquire dll which will be generated in location as shown below ( Sample ) :

C:\Users\jayakrishnak\source\repos\Plugin Library\Plugin Library\bin\Debug\Plugin Library.dll 


Ø  Open Plugin Registration tool in Tools folder as shown below :





Once you have the dll – open the Plugin Registration Tool from Tools folder and connect to Office 365 Online Instance ( Sample Picture below)




In the above picture – Select Show Advanced  will open up Regional Data centre details so that we can specify Data Centre location if known which reduces execution time and increases performance .

Ref Picture : 


Ø  Now Register the Assembly using Register New Assembly option as shown below ( select the dll which you have generated via visual studio to create new plugin assembly)







Once you add Assembly – go to Step 3 to specify Isolation mode. For Online Instances \Orgs – Isolation mode has to be Sandbox cause Sandbox execution is more secured and external references will be included only if it is secured.

Isolation Mode :

Sandbox  : If you specify your plugin to run in isolation mode – Sandbox – it allows more secured transactions and below actions cant be performed in Sandbox mode –

  • Access to the file system (C Drive)
  • system event log
  • certain network protocols
  • registry
  • You cannot access any other DLL’s
  • You cannot call any webservices from within a sandboxed plugin
  • IP addresses cannot be used
  •  Only the HTTP and HTTPS protocols are allowed.

None : In none mode – Security is less for transactions and you can include your local machine documents , images and even you can access other websites in None mode. 

  • In isolated mode you cannot call any external DLL’s\DLL’s in the GAC

Now navigating to Step 4 – Specify Location to store Plugin Assembly



Database – Plugin Assembly will be stored in Database and this option allows to transport plugin assemblies via solutions to different Orgs.
Disk – Plugin Assembly will be stored in local disk reducing efforts on database and transactions will be faster. But limitation is that Plugin Assembly can’t be exported to other orgs via Solution Export\Import

GAC – We can store assembly in a Global Assembly Cache so that all the Assemblies can be well Organized. But plugin assemblies can’t be exported using Solution.

Register the Assembly and Register New step to define when the plugin should Trigger and on behalf of which user it should trigger and specify whether Synchronous \ Asynchronous.
 I will discuss Plugin Execution Pipeline over next mail.







No comments:

Post a Comment