Develop Plugin with Sample Code

CategoriesDynamicsStartPlugin's

“Now we are going to implement a basic plugin,Plugin will stop the record creation if anyone creating record with name as “TestAccount”. Plugin will check the value entered in name field and if name filed contains “TestAccount” then plugin throw error alert.”

Need a visual studio to create a plugin, Use the below steps to write a Sample plug-in

Create a Visual Studio Project for the Plug-in

  1. Open Visual Studio 2019 and open a new Class Library (.NET Framework) project using .NET Framework 4.7.2

A screenshot of a social media post

Description automatically generated

-The name used for the project will be the name of the assembly. This tutorial uses the name SamplePlugin.

This image has an empty alt attribute; its file name is Untitled.png

  1. In Solution Explorer, right-click the References and select Add CRM core libararies from SDK bin folder from the context menu.

A screenshot of a cell phone

Description automatically generated

  1. In Solution Explorer, right-click the Class1.cs file and choose Rename in the context menu.

A screenshot of a cell phone

Description automatically generated

  1. Rename the Class1.cs file to StopDuplicate.cs
  2. When prompted, allow Visual Studio to re-name the class to match the file name.

A screenshot of a cell phone

Description automatically generated

  1. Add the following using statements to the top of the StopDuplicate.cs file:

using System.ServiceModel;

using Microsoft.Xrm.Sdk;

  1. Implement the IPlugin Interface by editing the class.

 Note

If you just type : IPlugin after the class name, Visual Studio will auto-suggest implementing a stub for the Execute Method.

public class FollowupPlugin : IPlugin

{

public void Execute(IServiceProvider serviceProvider)

{

throw new NotImplementedException();

}

}

  1. Replace the contents of the Execute method with the following code:

public void Execute(IServiceProvider serviceProvider)

{

// Obtain the execution context from the service provider.

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

// The InputParameters collection contains all the data passed in the message request.

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

string name = (string)entity[“name”];

if (“TestAccount”.Equals(name))

{

throw new InvalidPluginExecutionException(“Already Account exist with same name”);

}

}

Once completed need to build the project to get Assemblies. Code having any syntax error it won’t build the project.

A screenshot of a cell phone

Description automatically generated

If you see in project folder ->SamplePlugin->Bin you can able to see SamplePlugin.dll.

About the author

Never estimate the people based on the work, respect them how they implement the ideas.

Leave a Reply

Your email address will not be published. Required fields are marked *