{Step By Step}Create and Register Plugin In Dynamics CRM


1) To write a plugin we need to set up our machine environment

a. Download and install Microsoft visual studio.

b. Download Microsoft CRM Dynamics 365 SDK. After downloading, extract it.

2) First open visual studio and choose C# Class library as follow.

clip_image002

3) After creating project, need to add references to design plugins. Right click on references and click on add references. You can see references window, click on browse button and go to folder where you extracted CRM SDK select the required references. Go to SDK\Bin location of folder and select 2 DLL files

i. Microsoft.Crm.Sdk.Proxy.dll

ii. Microsoft.Xrm.Sdk.dll

Add these two files to references tick on them and click ok button.

clip_image004

clip_image006

4) Now add the following code to plugin.clip_image008

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Microsoft.Xrm.Sdk;

namespace PluginDesign

{

public class PreCreateAccount :IPlugin

{

public void Execute(IServiceProvider serviceProvider)

{

IPluginExecutionContext context = (IPluginExecutionContext)

serviceProvider.GetService(typeof(IPluginExecutionContext));

if (context.InputParameters.Contains(“Target”) &&

context.InputParameters[“Target”] is Entity)

{

Entity entity = (Entity)context.InputParameters[“Target”];

if (entity.LogicalName == “account”)

{

Entity account = entity;

String accountIdNumber = (String)account.Attributes[“new_accountid”];

if (entity.Attributes.Contains(“accountnumber”) == false)

{

Random rndgen = new Random();

entity.Attributes.Add(“accountnumber”, rndgen.Next().ToString());

}

else

{

throw new InvalidPluginExecutionException(“The account number can only be set by the system.”);

}

}

}

}

}

}

5) This code will generate random number and add it to Account number field of account when we create record of account entity.

6) After writing plugin, we need to register plugin on entity for which we designed it.

7) Go to CRM SDK extracted folder SDK\Tools\PluginRegistration at this location click on PluginRegistration Application it will open PluginRegistration Tool. Click on create new connection button. Enter your credentials and select online/office365 option click on Login button.

clip_image010

8) Click on Register button of menu bar and click on Register New Assembly.

clip_image012

9) On plugin registration window select location of assemply file and after that select respective plugin and finish process.

clip_image013

10) Now click on assembly of register plugin left click on plugin and register new step.

11) Select entity and type of message and select how to execute plugin and click on register.

clip_image015

Hope it helps and Happy CRMing!

Any problem in CRM – end user,Microsoft  partner or an individual?

Problem Area – Technical, Functional, Training, Development or consulting?

Please mail to deepesh@marvels.company