Sunday, March 3, 2024

Develop a custom API in Power Apps using plugins | Dynamics 365

A Comprehensive Guide to Building Custom APIs in Power Apps with Plugins

Introduction:

This blog explores building custom APIs within Power Apps by leveraging plugins. Custom APIs empower developers to expose specific functionalities of Dynamics 365, enabling seamless integration with external systems and services.
Scenario - We will develop a plugin to retrieve all case records from Dynamics 365. Subsequently, we'll establish a Custom API, associate it with the registered plugin, and invoke the API using Power Automate Flow.

Refer to this video for a detailed implementation guide for Developing, Configuring and Invoking Custom API


Output Snapshot -

 Understanding Custom APIs
A custom API (Application Programming Interface) is a way for developers to create their own interfaces that expose specific functionalities of a system or service. These custom APIs can then be used by other applications to interact with the original system in a controlled and secure manner. A "Custom API" is a tool that lets developers make their own Web APIs on the platform. This enables integration with external systems, services, or custom functionality within Dynamics 365. It allows developers to create their own API messages to handle complex business logic and integrate with external systems

 Methods for Custom API Creation in Power Apps

There are several approaches to creating custom APIs in Power Apps:

1. Power Apps: This method utilizes forms for data entry. It eliminates the need for separate tools but requires creating individual records for each custom API component.

2. Plugin Registration Tool: This user-friendly GUI tool streamlines the plugin development process.

3. Code-based Approach: With a grasp of the data model, developers can utilize tools like Postman to swiftly create custom APIs. Alternatively, they can build a custom interface for API creation.

4. Solution Files: For those employing Application Lifecycle Management (ALM) tools, custom API definitions can be created or modified using XML files within solution packages managed through source code repositories. These APIs are established upon importing the generated solution.

5. XrmToolBox- Custom API Manager - Install Custom API Manager and Custom API Tester to create and invoke the API using XrmToolBox



 Prerequisites
Before starting on the development of the custom API using a plugin, ensure you possess the necessary tools, licenses, and knowledge outlined below.
  1. Power Apps License
  2. Power Automate License
  3. Plugin/C# Development Knowledge
  4. Visual Studio (with .Net Framework 4.6.2 for plugin development)
  5. Plugin Registration Tool

Creating a Custom API in Power Apps

There are two options when creating a custom API: utilizing a new solution or leveraging an existing one. Here, we'll delve into the Power Apps method:

1. Navigate to your solution and select "New" > "More" > "Other" > "Custom API."

2. Provide details like Display Name, Name, Unique Name, Binding Type, and other relevant fields. This mirrors the process with the Plugin Registration Tool, just with a different interface.

Note: The unique name must commence with the Publisher prefix, such as "new_fieldname." If provided without the prefix, the Custom API cannot be saved and will result in the following error

3. Develop a plugin assembly and register it using the Plugin Registration Tool. You can reuse a previously created plugin assembly here.

4. Upon registering the assembly, establish the Plugin Type value for the custom API. This functions as a lookup property; locate the Plugin Type corresponding to the type generated during assembly registration.

5. Choose your plugin assembly in the Plugin Type field and click "Save."
Your form should look something like this:

Defining Request and Response Properties 

Custom APIs can incorporate request and response parameters. Here, we'll explore both:

Defining Request Properties  (Optional)
While a custom API can function without parameters, you can create as many as your logic requires to transmit essential data. We can add Request and Response parameter for this API but we are not sending any request here so this part is optional to create Request parameter

1. In your solution, click New > More > Other > Custom API Request Parameter from the drop-down.

2. Edit the fields to set the properties of your custom API Request Parameter. For more information see CustomAPIRequestParameter Table Columns

3. Click Save. Your form should look something like this:

Defining Response Properties 
Custom APIs can incorporate request and response parameters. Here, we'll focus on response properties:

1. Within your solution, navigate to "New" > "More" > "Other" > "Custom API Response Property."

2. Configure the properties of your response property by providing details like Display Name, Name, Unique Name, Type (e.g., String), and so on.

3. Click Save. Your form should look something like this:


Building and Registering a Plugin

Now, we'll delve into creating and registering the plugin assembly:

1. Launch Visual Studio and establish a project using the .NET Framework 4.6.2 Class Library template.

2. Integrate the required Microsoft.Xrm.Sdk assemblies and write your code to fetch cases using a Linq query (consider incorporating filter criteria for an optimized response).

3. Sign in to the plugin assembly and build the project.

4. Utilize the Plugin Registration Tool to register this plugin assembly.

5. Following registration, open the previously created Custom API and designate the registered plugin and assembly.

6. Access the solution within Dynamics and examine the API details for verification.

Link to download the plugin code (String Response) - here
Link to download the plugin code (JSON Response) - here

Plugin code and registering - Snapshot-


Invoking the Custom API with Power Automate

Here's how to leverage the custom API within a Power Automate flow:

1. Navigate to the flow page and initiate a new instant flow with a manual trigger.

2. Incorporate a new step and choose "Perform an Outbound action" from Microsoft Dataverse.

3. Input or select the unique name of the custom API created earlier in the "Action Name" field.

4. Manually test the flow and examine the response within the run history.

5. The second step of the flow should display the case records retrieved by the plugin. You can customize the response to show only the desired fields by implementing a filter within the plugin code.

Flow Structure below-

Result after the flow ran Successfully-

FAQs:

Q1: Can I create a custom API without using a plugin?
A1: Yes, Power Apps provides a code-based approach, allowing developers to swiftly create custom APIs using tools like Postman.

Q2: How can I optimize my plugin code for better performance?
A2: Consider applying filter criteria in your LINQ queries and optimizing your code for efficiency.

Q3: Are there any limitations to the number of request parameters in a custom API?
A3: While there's no strict limit, it's recommended to keep the number reasonable for better readability and maintenance.

Next Steps:
Stay tuned for the next part, where we'll delve into Building Custom APIs in Plugin Registration Tool with Plugins and Building Custom APIs with Solution Files. 

Next part - Create a custom API using the plug-in registration tool
Next part - Create a custom API with solution files

References - Microsoft Learn

No comments:

Post a Comment

Followers

Power Dynamix YouTube Videos