Implementation Of Custom API In Common Data Service


Microsoft has introduced a new way to communicate to Common Data Model through Custom API, assisting developers to integrate and communicate to Common Data Model more easily and securely.
To date, when we needed to implement some logic of integration with Common Data Model to some cross-domain system we usually used to implement a long chain which included below,
  • Action – It acted as a receiving point for data, and as well as a trigger point for logic
  • Plugin – It acted as a processing unit for incoming data, where the actual logic was implemented
And this implementation has certain drawbacks,
  • We cannot restrict an Action to a specific role, which means any user with any role can call the action, though we can restrict the execution of logic using certain other tricks
  • Action can be used by any developer to register any plugin code, and which at times used to change the entire logic processing, no restriction of stages exposed to register the plugin
  • No way to hide a Custom Action in Metadata Web Call
  • And Creation of Custom Action needed a technical knowledge of the system, as it cannot be created through Solutions in Portal
Microsoft recently introduced this unique concept of Custom Action wherein all the above shortcomings were fulfilled.

Steps to create a Custom API

  • Login to your PowerApps Portal which can be accessed over here.
  • Select your desired environment as we will need to create a solution in there
  • Create a solution with a suitable name

And the First step is to create a CUSTOM API 
Allowed Values
Binding Type
Global: Entity: Entity Collection
Usually, we select as Global, as we do not want it to be associate with a specific entity or collection
Bound Entity Logical Name
If we have selected the entity, then we must pass a schema name of the entity
Is Function
Yes: NO
We are setting it as NO as we will be calling it through a custom API
Is Private
Yes: NO
It helps to hide the Custom API from been exposed in Metadata
Allowed Custom Processing Step Type
Sync: Async
Execute Privilege Name
Schema name of security role
It helps to restrict calling of Custom API only through specific roles

Post creation of Custom API, need to create desired set of

  • Custom Request Parameters
    Request Parameters is the value that will be passed to Custom API while been called through any source. It enables us to pass custom values which will be processed by our logic

  • Custom Response Property
    The response is nothing but the output which will be passed/responded back to source post execution of custom logic

Steps to implement Custom Logic

  • Need to create the Custom Assembly in Visual Studio
  • The same we do for writing a custom plugin in Dynamics CRM
  • Register the assembly using the Plugin Registration Tool
  • And then need to bind the plugin class file to the Custom API
  1. public class ContactQualify : IPlugin  
  2.    {  
  3.        public void Execute(IServiceProvider serviceProvider)  
  4.        {  
  5.            ITracingService tracingService =  (ITracingService)serviceProvider.GetService(typeof(ITracingService));  
  6.            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));            
  8.            {  
  11.                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));  
  12.                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);  
  14.                try  
  15.                {  
  16.                    tracingService.Trace(context.InputParameters["sum_Target"].ToString());  
  17.                    Entity userDetails = service.Retrieve("systemuser", context.UserId, new Microsoft.Xrm.Sdk.Query.ColumnSet(true));  
  18.                    tracingService.Trace(context.MessageName);  
  19.                    tracingService.Trace(context.Stage.ToString());  
  20.                    context.OutputParameters["sum_CustomParameter2"] = userDetails.GetAttributeValue<string>("fullname");  
  22.                    //  throw new InvalidPluginExecutionException("Follow the link to get pass through Exception during bulk record creation :) ");  
  23.                }  
  24.                catch (Exception ex)  
  25.                {  
  26.                    tracingService.Trace("FollowUpPlugin: {0}", ex.ToString());  
  27.                    throw;  
  28.                }  
  29.            }  
  30.        }  
  31.    }  

Steps to execute the Custom API

  • Write a simple console application
  • Connect to Common Data Model using organization service and valid credentials
  • And just use the below code to execute the Custom API
  1. try  
  2.   {  
  3.       if (service != null)  
  4.       {  
  5.           var req = new OrganizationRequest("sum_CustomAPI2")  //Name of Custom API
  6.           {  
  7.               ["sum_Target"] = "Sumit"  //Input Parameter
  8.           };  
  9.           var resp = service.Execute(req);  //resp will have output parameter
  11.       }  
  12.   }  
  14.   catch (Exception ex)  
  15.   {  
  16.       Console.Write(ex.ToString());  
  17.   }  

Similar Articles