Showing posts with label Write a Plugin for the Custom API. Show all posts
Showing posts with label Write a Plugin for the Custom API. Show all posts

Friday, December 30, 2022

Write a Plug-in for the Custom API

Writing a plug-in to implement the main operation for the Custom API isn't different from writing any other kind of plug-in, except that we do not use the Plug-in Registration tool to set a specific step.

We need to know the following information follow the links below:

  • The name of the message
  • The names and types of the request parameters and response properties.

The Request Parameter values will be included in the InputParameters.

We need to set the values for the Response Properties in the OutputParameters.

The following is a simple plug-in that reverses the characters  StringParameter and returns the result as the StringProperty.

Open the Plugin Solution and add the required references and class as shown.



//Inside the class add the below code -> Save -> Build the solution:

using System;
using System.Linq;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
 
namespace MyPluginSolution
{
    public class tcc_CustomAPIExample : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            // Obtain the tracing service
            ITracingService tracingService =
            (ITracingService)serviceProvider.GetService(typeof(ITracingService));
 
            // Obtain the execution context from the service provider. 
            IPluginExecutionContext context = (IPluginExecutionContext)
                serviceProvider.GetService(typeof(IPluginExecutionContext));
 
            if (context.MessageName.Equals("tcc_CustomAPIExample") && context.Stage.Equals(30))
            {
 
                try
                {
                    string input = (string)context.InputParameters["StringParameter"];
 
                    if (!string.IsNullOrEmpty(input))
                    {
                        //Simply reversing the characters of the string
                        context.OutputParameters["StringProperty"] = new string(input.Reverse().ToArray());
                    }
                }
                catch (Exception ex)
                {
                    tracingService.Trace("tcc_CustomAPIExample: {0}", ex.ToString());
                    throw new InvalidPluginExecutionException("An error occurred in tcc_CustomAPIExample.", ex);
                }
            }
            else
            {
                tracingService.Trace("tcc_CustomAPIExample plug-in is not associated with the expected message or is not registered for the main operation.");
            }
        }
    }

}

 

Register the Plugin, No need to register steps for this plugin:

- Open the Plugin Registration Tool (PRT) -> Login -> select the Dynamics 365 instance.


- Click Register -> Register new Assembly



- Select the dll file and click on Register selected plugins button



- It will look like below:


Navigate back to Custom API Creation Page to Update the Custom API and Update the Plugin Type attribute 



Thank you !!!
Happy Learning...!!!!