Custom plugin execution from Power Pages

Loading

Power Pages (formerly Power Apps Portals) provide a dynamic platform for building low-code websites backed by Dataverse. However, when you need to implement complex business logic, server-side validation, or custom processing, one powerful option is using custom plugins.

This guide will walk you step-by-step through the execution of a custom plugin from Power Pages, including how plugins are registered, triggered, and how to invoke them from portal forms, Web API calls, and more.


1. What is a Plugin in Dataverse?

A plugin in Dataverse is a compiled .NET class (typically written in C#) that executes synchronously or asynchronously in response to events such as:

  • Create, Update, Delete of a Dataverse record
  • Retrieve or RetrieveMultiple operations
  • Associate/Disassociate, and more

In the Power Pages context, these plugins are invoked indirectly when users interact with portal elements like forms or custom API calls.


2. Use Cases for Plugins in Power Pages

Use CaseDescription
Business rule validationBlock submission if logic conditions fail
Custom calculationsCompute values server-side before saving
Record duplication checksAvoid duplicate entries
Logging or audit trackingSave logs on certain updates
Calling external systemsPush/pull data via APIs (server-to-server)

3. Prerequisites

  • Access to Dataverse and Power Platform Admin Center
  • Plugin development setup with Visual Studio
  • Plugin Registration Tool from XrmToolBox or Power Platform Tools
  • System Customizer/Admin role in Dataverse
  • Power Pages site connected to the same Dataverse environment

4. Creating a Plugin (Step-by-Step)

Step 1: Create the Plugin Class in Visual Studio

using Microsoft.Xrm.Sdk;
using System;

public class ValidateBeforeCreate : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context =
(IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

IOrganizationServiceFactory factory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

IOrganizationService service = factory.CreateOrganizationService(context.UserId);

if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity entity)
{
string email = entity.GetAttributeValue<string>("emailaddress1");

if (email != null && email.EndsWith("@blocked.com"))
{
throw new InvalidPluginExecutionException("Email domain is not allowed.");
}
}
}
}

This plugin checks if a submitted email ends with @blocked.com and blocks the submission.


Step 2: Compile the Plugin DLL

  • Compile the plugin class library
  • Create a strong-name signed assembly (.snk file)

Step 3: Register the Plugin in Dataverse

Use Plugin Registration Tool or Power Platform Tools in Visual Studio:

  • Register Assembly
  • Register Step:
    • Message: Create
    • Primary Entity: contact (or your custom table)
    • Execution Mode: Synchronous
    • Execution Pipeline Stage: Pre-Validation or Pre-Operation
    • Filtering Attributes: optional
    • Execution Order: 1

5. Trigger Plugin via Power Pages

Now that the plugin is registered, you can trigger it indirectly from Power Pages using:

A. Basic/Advanced Forms

If your plugin is registered on the Create or Update message, it will automatically be triggered when a user submits a Basic Form or Advanced Form mapped to that entity.

🔹 Example:

  • User submits a registration form.
  • Email validation plugin is triggered.
  • If logic fails, error is shown.

B. JavaScript + Web API (CRUD)

You can also trigger plugin logic via client-side JavaScript that calls the Power Pages Web API.

Example:

var data = {
"firstname": "Irfan",
"emailaddress1": "irfan@blocked.com"
};

$.ajax({
type: "POST",
url: "/_api/contacts",
contentType: "application/json",
data: JSON.stringify(data),
success: function (response) {
alert("Created successfully!");
},
error: function (xhr) {
alert("Plugin Error: " + xhr.responseJSON.error.message);
}
});

If the plugin logic fails, the user will see an error returned by the API.


6. Debugging Plugins

To debug a plugin:

  • Use Plugin Profiler from Plugin Registration Tool
  • Enable plugin profiling, reproduce the issue on the portal
  • Download execution profile, and debug in Visual Studio

This is essential to catch errors during plugin execution, especially when users submit data from Power Pages.


7. Best Practices

Best PracticeDescription
Use pre-validation or pre-operationTo catch invalid submissions early
Avoid long-running codePlugins should execute quickly (< 2 seconds)
Throw user-friendly errorsUse InvalidPluginExecutionException
Avoid UI-specific logic in pluginsPlugins are backend only
Use tracingWrite trace logs to debug issues
Register under appropriate usersUse SYSTEM or specific users for consistency

8. Real-World Scenarios

Form Validation Plugin

Trigger validation before user form submits data, e.g., only allow records with valid PAN numbers.

External API Call Plugin

Trigger a plugin that sends form data to an external CRM or API upon creation.

Custom Email Notification

On form submission, the plugin sends custom emails (e.g., acknowledgment) instead of using Power Automate.


9. Security Considerations

  • Plugins run in Dataverse, not on the portal—this enhances security
  • Validate all incoming data
  • Never expose plugin logic directly to the frontend
  • Always check for user privileges via IPluginExecutionContext

Leave a Reply

Your email address will not be published. Required fields are marked *