Dataverse Custom APIs are the preferred approach for exposing reusable business logic in Microsoft Dataverse.
In this article, we'll build our first Dataverse Custom API from scratch.
By the end of this guide, you will learn how to:
- Create a Custom API
- Configure Request Parameters
- Configure Response Parameters
- Develop a Plug-in
- Register the Assembly
- Test the Custom API
Business Scenario
Let's assume we need an API that returns customer (contact) details based on a Contact ID.
Input:
ContactId
Output:
FullName
Phone
Prerequisites:
• Visual Studio 2022
• .NET Framework 4.6.2 Class Library
• Microsoft.CrmSdk.CoreAssemblies NuGet Package
• Dataverse Environment
• System Administrator or System Customizer Role
• Plugin Registration Tool
Follow the below steps after the Prerequisites to create your first Dataverse Custom API.
Step 1: Open Visual Studio 2022.
Step 2: Click on Create a new project.
Step 3: Search for Class Library.
Select Class Library (.NET Framework) and Click on Next.
Step 4: Provide the below details and click on Create.
Project name |
Provide the name for the Project (Example: Dataverse.CustomAPI.GetCustomerDetails) |
Location |
Choose the required Location |
Framework |
Choose .NET Framework 4.6.2 |
Step 5: Change the Class name form Class1.cs to GetCustomerDetails.cs.
Step 6: Right click on References and Click on Manage NuGet Packages.
Step 7: Click on Browse and Search for Microsoft.CrmSdk.CoreAssemblies.
Click on Install.
Step 8: Click on Apply.
Step 9: Click on I Accept.
Once successfully package is installed, you can see the below message.
Step 10: Add the below to the namespace references.
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
Step 11: Copy and Paste the below code.
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
namespace Dataverse.CustomAPI.GetCustomerDetails
{
public class GetCustomerDetails : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
// Get the ContactId from the input parameters
Guid contactId = (Guid)context.InputParameters["ContactId"];
// Retrieve the contact record from Dataverse based on the provided ContactId
Entity contact = service.Retrieve(
"contact",
contactId,
new ColumnSet("fullname", "emailaddress1", "telephone1"));
// Set the output parameters
context.OutputParameters["FullName"] =
contact.GetAttributeValue<string>("fullname");
context.OutputParameters["Email"] =
contact.GetAttributeValue<string>("emailaddress1");
context.OutputParameters["Phone"] =
contact.GetAttributeValue<string>("telephone1");
}
}
}
Final code looks like the below,
Step 12: Click on the project and press Alt + Enter. Click on Signing.
Join the conversation! Your thoughts help the community grow.