Welcome to Dynamics in Motion

Beginner’s Guide to Writing and Implementing Dynamics 365 Plugins: Simplifying the Process

Beginner’s Guide to Writing and Implementing Dynamics 365 Plugins: Simplifying the Process

Title: Demystifying the Process of Writing a Simple Dynamics 365 Plugin: A Guide for Beginners

If you’re a fledgeling developer or a business owner looking to enhance your programmatic abilities, learning how to write a simple Dynamics 365 plugin is an invaluable skill. Dynamics 365, Microsoft’s suite of business applications, offers developers immense capabilities to customise and extend functionality with plugins. This blog post aims to guide you through the process.

1. UNDERSTANDING DYNAMICS 365 PLUGINS:

Before we dive into the practical aspect, it’s essential to understand what Dynamics 365 plugins are. They are custom business logic components that integrate seamlessly into the system’s event processing pipeline to modify or extend the standard behaviour.

2. PRE-REQUISITES:

To develop Microsoft Dynamics 365 plugins, you need a few essentials:
– Knowledge of C# or .NET Framework.
– Visual Studio (latest version)
– Dynamics 365 Software Development Kit (SDK).

3. WRITING YOUR FIRST PLUGIN:

Here is a step-by-step guide to creating a simple Dynamics 365 plugin.

3.1 CREATE A NEW PROJECT IN VISUAL STUDIO:

Start by launching Visual Studio. Click on ‘File’, select ‘New’, then ‘Project’. From the Project Type window, select ‘Class Library’ under Visual C#. Name the project, choose the location, and click ‘OK’.

3.2 ADD MICROSOFT.XRM.SDK REFERENCES:

To create the plugin, we need the ‘Microsoft.Xrm.Sdk.dll’ reference. Right-click on ‘References’, select ‘Add Reference’ and locate this DLL in the Dynamics 365 SDK you downloaded earlier.

3.3 WRITING THE PLUGIN CODE:

Next, start writing your plugin code. Begin by importing the necessary namespaces first. You will need to use ‘Microsoft.Xrm.Sdk’ and ‘System’.

Following this, declare the plugin class. Have it publicly inherit from the ‘IPlugin’ interface. The ‘Execute’ method, a requisite of the ‘IPlugin’ interface, must be included in this class.

Here’s an example of a simple plugin that creates a task every time a new account is created:

“`
using System;
using Microsoft.Xrm.Sdk;

public class AccountCreatePlugin : 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”)
{
try
{
var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var service = serviceFactory.CreateOrganizationService(context.UserId);

Entity task = new Entity(“task”);
task[“subject”] = “Follow up on new account creation”;
task[“description”] = “Account name: “+ entity[“name”];
task[“scheduledend”] = DateTime.Now.AddDays(7);
service.Create(task);
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
}
}
}
}
“`

3.4 BUILD THE SOLUTION:

After writing the code, build the solution. If the build succeeds, your DLL will be generated in the project’s bin\debug directory.

4. DEPLOYING THE PLUGIN:

To deploy the simple Dynamics 365 plugin, you need a Plugin Registration Tool. It’s included in the SDK. Register a new step for the ‘Create’ message on the ‘Account’ entity to link your plugin.

5. TESTING THE PLUGIN:

Finally, create a new ‘Account’ in Dynamics 365. If the plugin deploys correctly, a new task titled “Follow up on new account creation” will be created.

In conclusion, learning to write a simple Dynamics 365 plugin can broaden your developmental capabilities. Hopefully, this article simplifies the process and serves as a starting point for your journey in customising Dynamics 365. Happy coding!

Leave a Reply

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

Search