Microsoft Dynamics 365 provides numerous extraordinary out of the box (OOB) properties. However, numerous business scenarios demand enhancements to the OOB framework that require executing customized logic. This customization can be accomplished in different ways, including Java Script, Custom Workflows, Custom Reporting, Scheduling Jobs, writing Plugins, etc. Plugins are presumably the most common and are a top choice of developers with regards to executing customized logic around entity actions such as Create, Update, Delete, Associate, and Disassociate.
A plugin is a class library that can be written in Dot Net platform-supported languages, including C# and Visual Basic. Class libraries - which are compiled into a DLL record (and more than one plugin can be essential for a solitary DLL document - should be enlisted in the Dynamics 365 environment. Customized logic in plugins coordinates with Dynamics 365 to perform required business processes.
First of all, we want an IDE to get everything rolling with plugin development. In case you don't already have them, download a Visual Studio and Dot Net Framework from Microsoft's official website. The latest version of Dynamics 365 can support customized code made with Framework version 4.6.2 or later.
Then, in case you don't already have it, now is the right time to Install Microsoft Dynamics 365 Customer Engagement Developer Guide (recently known as CRM SDK). As of Dynamics 365 v9, there could be presently not a Microsoft-provided download for the Software Development Kit (SDK). But, by utilizing either PowerShell or NuGet, we can in any case install the developer tools, assemblies, and code samples that are installed as part of the SDK for Dynamics 365 v9.
Now we want a placeholder for our customized code, so pick a development language and then create another Solution and Project in Visual Studio. Note that we cannot generate an executable file without having a Project and Solution.
Next add a "Class" and determine a class name for the plugin. This is where we would compose a code to implement business logic.(using Microsoft XRM SDK Assembly)
The SDK assemblies are available as NuGet packages that you can straightforwardly use in your Visual Studio projects.
To register the plugin with Dynamics 365, we can utilize the Plugin Registration Tool by installing it with NuGet or by using XRM Toolbox Plugin Registration.
Start the process by entering all necessary connection information to establish a connection with the required Dynamics 365 organization. While registering new plugin step, we select Primary Entity and the Message activities (Create, Update, Delete, Associate, and Disassociate) for the plugin that is being Created/Updated.
o call a third-party web API in a .NET plugin and handle the JSON response using Newtonsoft.Json (Json.NET), follow these steps:
Install Newtonsoft.Json: Add the Newtonsoft.Json NuGet package to your project if it's not already installed. You can install it via the Package Manager Console:
shell
Copy code
Install-Package Newtonsoft.Json
Set Up HttpClient: Use HttpClient to send requests to the API. Configure headers, including any necessary authentication.
Define Model Classes: Define C# classes that map to the JSON response structure. This allows you to deserialize JSON into strongly typed objects.
Make the API Request and Deserialize JSON:
Send the API request.
Deserialize the JSON response using JsonConvert.DeserializeObject<T>().
Handle Errors: Use try-catch to manage exceptions, such as network issues or JSON parsing errors.
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Workflow;
using Newtonsoft.Json;
using System;
using System.Net.Http;
namespace PARS.BasePlugin
{
public class APICALL: IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
//IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
// Get a reference to the Organization service.
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
Entity target = null;
try
{
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
target = context.InputParameters["Target"] as Entity;
}
if (target == null)
return;
if (context.MessageName.ToLower() == "update")
{
#region Call API
try
{
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "APIURL" );
var json = JsonConvert.SerializeObject(apiparameters);
var content = new StringContent(json, UnicodeEncoding.UTF8, "application/json");
request.Content = content;
var response = client.SendAsync(request).Result;
apiresponse = JsonConvert.DeserializeObject<T>(response.Content.ReadAsStringAsync().Result);
if (!response.IsSuccessStatusCode)
throw new Exception($"Http Status Code: {response.StatusCode}");
}
catch (Exception ex)
{
throw new Exception($"Error in API Call Message : {ex.Message}");
}
#endregion
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
}
}
}