Dataverse: Create Custom API and Implement using plugin C#

In this blog, we’ll see how to create custom API in maker portal and write plugin to implement it. We’ll take the example of refreshing roll up field on demand. This API will take 3 parameters:

  1. TargetEntity: Name of the entity containing the roll up field
  2. TargetId: GUID of the record
  3. Field: Name of the roll up field to be refreshed

In the plugin we’ll write the code to execute request to refresh roll up field. We’ll test the custom API using Postman.
So, let’s get started.

Go to Solutions -> Click on New -> Select Custom API.

Fill the details of the custom API record as shown below:

Once the custom API is created, in the Solution, Click New -> Select Custom API Request Parameter.

The first parameter TargetEntity is of String Type. Fill the details as shown below:

Similarly, create the second parameter TargetId which is of String Type.

and the 3rd parameter Field which is also of String type.

Once the custom API and parameters have been created, Go to Solution -> Click Add Existing -> Select Custom API to include the custom API and its request parameters in the solution.

Now, we can write the plugin which would execute request for refreshing roll up field. Below is the code:

namespace CIDBIO.Plugins.OpportunityProduct
    public class CalculateRollupField : IPlugin
        public void Execute(IServiceProvider serviceProvider)
            // Obtain the tracing service
            ITracingService tracingService =

            // Obtain the execution context from the service provider.  
            IPluginExecutionContext context = (IPluginExecutionContext)

            string fieldName = (string)context.InputParameters["cid_Field"];
            var targetEntity = (string)context.InputParameters["cid_TargetEntity"];
            var targetId = (string)context.InputParameters["cid_TargetId"];
            IOrganizationServiceFactory serviceFactory =
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                CalculateRollupFieldRequest crfrRequested = new CalculateRollupFieldRequest
                    Target = new EntityReference(targetEntity, new Guid(targetId)),
                    FieldName = fieldName

                CalculateRollupFieldResponse responseRequested = (CalculateRollupFieldResponse)service.Execute(crfrRequested);
            catch (Exception ex)
                tracingService.Trace("CalculateRollupField: {0}", ex.ToString());

After writing the plugin, deploy the plugin assembly.

Then update Plugin Type field on the custom API with the plugin just deployed as shown below:

Now, we can test the custom API. Before executing the custom API, we can see that the roll up field No of Contacts was refreshed at 4:22PM.

Using Postman, we can test or execute the custom API as shown below:

After executing custom API, we can see that the roll up field has been refreshed.

This is a simple example of how we can use custom API and implement the logic using plugin. Hope it helps !!

4 thoughts on “Dataverse: Create Custom API and Implement using plugin C#

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.