Dataverse Plug-in Development: Pre-Validation, Pre-Operation and Post-Operation Explained

BEYONDTHEBYTES · DYNAMICS CRM DEVELOPER GUIDE

Dataverse Plug-in Development: Pre-Validation, Pre-Operation and Post-Operation Explained

Understanding the Dataverse plug-in execution pipeline is essential for building reliable Dynamics 365 customizations. Choosing the correct stage helps you validate data, modify records safely and avoid unnecessary database operations.

What is the Dataverse plug-in pipeline?

A Dataverse plug-in is custom .NET code registered to run when a platform message occurs, such as Create, Update, Delete, Assign or Associate. The execution pipeline provides stages where your code can inspect or influence the operation.

1. Pre-Validation stage

Pre-Validation runs before the main database transaction in the usual synchronous pipeline. It is a good place to reject an operation early when a business rule is violated. For example, prevent a Lead from being created when a required external reference is already in use.

2. Pre-Operation stage

Pre-Operation runs inside the database transaction. Use it to modify values in the Target entity before Dataverse saves the row. This is often preferable to updating the same row again from Post-Operation.

3. Post-Operation stage

Post-Operation runs after the main operation. Synchronous Post-Operation is still within the transaction, while asynchronous Post-Operation runs after the transaction has completed. Use it when you need the created row ID or want to initiate work that does not need to block the user’s save.

C# example: set a default description before saving

This simplified Update plug-in checks the Target entity and fills a description only when the caller has not supplied one. Register it synchronously in Pre-Operation on the Account Update message, and filter the step to the relevant columns where appropriate.

using Microsoft.Xrm.Sdk;

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

        if (!string.Equals(context.MessageName, "Update",
            StringComparison.OrdinalIgnoreCase))
            return;

        if (!context.InputParameters.TryGetValue("Target", out var value)
            || value is not Entity target
            || target.LogicalName != "account")
            return;

        if (!target.Contains("description"))
        {
            target["description"] =
                "Account updated through the Dataverse plug-in pipeline.";
        }
    }
}

Because this example changes the Target in Pre-Operation, it does not need to call Update on the same Account. In production, add tracing, meaningful exception messages, secure configuration where needed and unit tests for the business rule.

How to register and test the plug-in

  • Build the class library against a supported Dataverse plug-in target framework.
  • Register the assembly using the Plug-in Registration Tool or your deployment pipeline.
  • Create a step for the Update message and Account table.
  • Select Pre-Operation and Synchronous execution for this example.
  • Use filtering attributes to avoid running the step for unrelated updates.
  • Test with a dedicated development environment and inspect Plug-in Trace Log entries.

Common mistakes to avoid

  • Registering on every Update without filtering attributes.
  • Retrieving or updating records repeatedly when the Target already contains the required values.
  • Using synchronous plug-ins for long-running external HTTP calls.
  • Throwing generic exceptions without useful, safe error messages.
  • Ignoring execution depth and accidentally creating recursive updates.
  • Not testing with the same security roles and data shape used by real users.

Key takeaway

Use Pre-Validation to reject invalid requests early, Pre-Operation to adjust values before the save, and Post-Operation when you need the result of the completed operation. For long-running integrations, prefer asynchronous processing or Azure messaging rather than delaying the CRM transaction.

BeyondTheBytes — practical Dynamics 365, Dataverse and Azure developer tutorials with real implementation examples.