In previous blog we have written .Net plugin code which will run in Main operation of Execution Pipeline and updated plugin type field on Custom API record. Today’s blog we will write plugin code and register on Pre-Operation event on Custom API message.
Custom API does support Pre-Event operation unlike Custom Action. Create new class file inside same class library and add below code.
if (context.MessageName.Equals("cr234_TestCustomAPI") && context.Stage.Equals(20))
{
try
{
string dataRequest = string.Empty;
if (context.InputParameters.Contains("DataRequest") && context.InputParameters["DataRequest"] != null)
{
dataRequest = (string)context.InputParameters["DataRequest"];
context.InputParameters["DataRequest"] = "472aa6d8-c133-eb11-a813-000d3af020c4";
}
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occurred in PreEventTestCustomAPI.", ex);
}
catch (Exception ex)
{
tracingService.Trace("PreEventTestCustomAPI: {0}", ex.ToString());
throw;
}
}
Key points
- First we are checking Message name from context “context.MessageName.Equals(“cr234_TestCustomAPI”)”
- Next we are checking in context stage is 20 which is Pre-Operation “context.Stage.Equals(20)”
- I am overriding the input parameter and chaning the record guid to different record guid. It is hard coded for testing purpose
- Make sure you deploy this code and register steps on Pre-Operation on Custom API message
- Now when you execute your Custom API, you will see contact details related to Pre-Operation contact record guid

Custom API has an Execute Privilege Name (ExecutePrivilegeName
) property which will let you to control your Custom API permission meaning you can only allow users to execute your Custom API who has certail privilege.
For example I have updated my Custom API record to only allow users who has “prvCreateLead” access. Now when I execute this CUstom API with user who does not have Create Privilege on Lead entity he should get an error.

System.ServiceModel.FaultException`1: ‘Principal user (Id=26adf271-1a02-eb11-a812-000d3af01a07, type=8, roleCount=2, privilegeCount=156, accessMode=0), is missing prvCreateLead privilege (Id=a41f965b-2619-4c73-8625-da31945cccc7) on OTC=4 for entity ‘lead’.
Hope this helps!
One thought on “{Microsoft Dataverse} Explore Custom API feature Part-4 in Dynamics 365”