Welcome to Dynamics in Motion

Mastering the Art of Customising Dynamics 365: A Simple Guide to Writing Effective Plugins

Mastering the Art of Customising Dynamics 365: A Simple Guide to Writing Effective Plugins

Title: How to Write a Simple Yet Effective Dynamics 365 Plugin

Introduction

In today’s era of digital transformation within businesses, Microsoft’s Dynamics 365 has become an invaluable tool. This state-of-the-art product suite brings together CRM and ERP capabilities into one integrated cloud solution. One of its most significant advantages is the ability to customize it according to specific business needs. A key approach to achieving this customisation is by writing plugins- a subject we’ll delve into in this article.

What are Dynamics 365 Plugins?

Plugins in Dynamics 365 are custom-made business logic code that can be used to modify or extend the standard behaviour of the system. They are server-side codes triggered by event handlers to perform a variety of functions such as data integration, data validation, and modification of system behaviour.

Writing your First Dynamics 365 Plugin

Before commencing on writing the plugin, ensure you have the following prerequisites:

1. Microsoft Visual Studio installed in your system.
2. Dynamics 365 Software Development Kit (SDK).
3. Access to a Dynamics 365 instance.

Step 1: Create a New Project

Open the Visual Studio and click on ‘File’ > ‘New’ > ‘Project.’ From the new project dialog box, select ‘Visual C#’ > ‘Class Library (.NET Framework)’ and name your project.

Step 2: Add References

You must add necessary references to work with the Dynamics 365 object model. To do this, right-click on ‘References’ > ‘Add Reference.’ Browse through and add the following assemblies from the SDK bin folder:

1. Microsoft.Xrm.Sdk
2. Microsoft.Crm.Sdk.Proxy

Step 3: Implement IPlugin Interface

To implement the IPlugin interface, replace namespace and class code with the following lines of code, ensuring you replace ‘MyPlugin’ with your chosen plugin name:

“`C#
using System;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;

namespace MyPlugin
{
public class MyPlugin: IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{

}
}
}
“`

Step 4: Writing Plugin Logic

The real business logic of the plugin is written inside the Execute method. The following example shows a simple code which is triggered on creation of a new account and sets the account’s name to “Hello World”.

“`C#
public void Execute(IServiceProvider serviceProvider)
{
var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var service = factory.CreateOrganizationService(context.UserId);

if (context.InputParameters.Contains(“Target”) && context.InputParameters[“Target”] is Entity)
{
Entity entity = (Entity)context.InputParameters[“Target”];
try
{
if (entity.LogicalName != “account”) return;
if (context.MessageName != “Create”) return;

entity[“name”] = “Hello World”;
}
catch (FaultException ex)
{
throw new InvalidPluginExecutionException(“An error occurred in the plug-in.”, ex);
}
}
}
“`
Step 5: Build and Register the Plugin

After writing your code, build your solution (Build > Build Solution). Use Plugin Registration Tool from the SDK tools to register your plugin assembly with the Dynamics 365 instance.

Conclusion

Dynamics 365 plugins provide a powerful way to customise and extend the functionality of your Dynamics 365 applications. While our example above demonstrates a simple plugin writing process, remember that as your needs become more complex, so can your plugins. As long as you adhere to best practices, the possibilities for creating plugins to tailor your Dynamics 365 system to your specific requirements are endless.

Leave a Reply

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

Search