Dataverse Plug-in Execution Context: Understand ParentContext, Depth and Correlation

The plug-in execution context contains information about the current message, user, stage, input parameters and execution chain. Reading it correctly helps a plug-in make decisions based on the operation it is handling rather than assumptions about the caller.

Background

Inspect the Dataverse plug-in execution context, understand depth and parent context, and use correlation identifiers to troubleshoot nested operations.

Step-by-step guide

  1. Capture MessageName, Stage, Mode, Depth, PrimaryEntityName and CorrelationId at the start of execution.
  2. Read only the InputParameters that are valid for the registered message and stage.
  3. Use ParentContext for diagnostic context when the current operation was triggered by another platform operation.
  4. Use Depth as a diagnostic signal for recursion, not as the only business rule for skipping logic.
  5. Use CorrelationId and tracing to connect related plug-in executions during investigation.

Example

var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
tracing.Trace("Message={0}; Stage={1}; Mode={2}; Depth={3}; Entity={4}; Correlation={5}",
    context.MessageName, context.Stage, context.Mode, context.Depth,
    context.PrimaryEntityName, context.CorrelationId);
if (context.ParentContext != null)
    tracing.Trace("Parent message={0}; Parent stage={1}",
        context.ParentContext.MessageName, context.ParentContext.Stage);

Pitfalls and recommendations

  • Do not hard-code assumptions that a specific parent message always exists.
  • Do not use Depth alone as a substitute for a correct recursion-safe design.
  • Avoid logging sensitive input parameter values.

Validation

Trigger direct and nested operations, verify context fields at each stage and confirm correlation traces are useful.

Conclusion

Keep the design aligned with the platform’s supported model, validate behaviour in a representative environment and document how the team will operate and troubleshoot it.