Build C# plugins with pre/post operation steps and execution pipeline
✓Works with OpenClaudeYou are a Dynamics 365 developer building custom C# plugins to intercept and manipulate data during CRM operations.
What to check first
- Verify Dynamics 365 SDK is installed: check your .csproj file contains
Microsoft.CrmSdk.CoreAssembliesNuGet package - Confirm plugin registration tool is available:
C:\Program Files\Microsoft Dynamics CRM\Tools\PluginRegistrationTool.exe(or latest version path) - Check your plugin assembly targets .NET Framework 4.6.2 or higher (or .NET 6.0 for newer SDK versions)
Steps
- Create a new C# class library project targeting .NET Framework 4.6.2
- Install
Microsoft.CrmSdk.CoreAssembliesNuGet package (Install-Package Microsoft.CrmSdk.CoreAssemblies) - Inherit your class from
IPlugininterface and implement theExecute(IServiceProvider serviceProvider)method - Extract the execution context using
serviceProvider.GetService(typeof(IPluginExecutionContext))cast toIPluginExecutionContext - Determine execution stage (Pre = 10, Post = 40) via
executionContext.Stageproperty - Check
executionContext.Depthto prevent infinite loops (typically avoid running if Depth > 1) - Retrieve the entity from
executionContext.InputParameters["Target"]asEntitytype - Access organization service via
serviceProvider.GetService(typeof(IOrganizationServiceFactory))to perform CRM operations - Register the plugin using Plugin Registration Tool, specifying the message (Create, Update, Delete, etc.), entity, and stage (Pre/Post)
Code
using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
public class AccountPreCreatePlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
// Only execute on Create message, Pre-operation stage
if (context.MessageName == "Create" && context.Stage == 10)
{
// Prevent infinite recursion
if (context.Depth > 1)
return;
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
// Business logic: Set default account rating
if (!entity.Contains("account
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Dynamics 365 Skills
Other Claude Code skills in the same category — free to download.
D365 Power Automate
Create Power Automate flows for Dynamics 365 automation
D365 Model-Driven App
Build model-driven apps with forms, views, and business rules
D365 Web Resources
Create JavaScript web resources for form customization
D365 FetchXML
Write FetchXML queries for advanced data retrieval and reporting
D365 Business Central
Develop AL extensions for Dynamics 365 Business Central
D365 Finance & Operations
Develop X++ customizations for Dynamics 365 Finance & Operations
D365 PCF Control
Build Power Apps Component Framework custom controls
D365 Dataverse API
Interact with Dataverse Web API for CRUD and batch operations
Want a Dynamics 365 skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.