Plugin Code Generation - rezanid/xrmtools GitHub Wiki

Xrm Tools leverages your plugin definitions to automatically generate boilerplate code, simplifying development.

Benefits

  • Typed entities for Target/Image
  • Dependency injection
  • Utility methods for Custom APIs
  • Customizable Scriban templates

How to Enable Code Generation

1. Enable Generation

Right-click your plugin class file → Enable Xrm Plugin Code Generation to enable.

2. Save to Trigger

Upon saving (Ctrl+S), Xrm Tools generates:

  • Typed properties for Target/Image.
  • InjectDependencies method (if [Dependency] attributes used).
  • Initialization logic.

Generated code is nested under your original class file (e.g., ContactCreatePlugin.Generated.cs).

Example of generated code (partial) for a plugin. The *.Generated.cs file contains code that Xrm Tools creates – you typically should not edit this file, as it will be regenerated whenever you save the main plugin file.

[!Warning] Do not manually edit the generated .Generated.cs file. Treat it as a build artifact.

3. Implement Business Logic

Use generated properties and methods in your original partial class file:

public void Execute(IServiceProvider serviceProvider)
{
    Initialize(serviceProvider);

    Target.Description = $"Hello {Target.FirstName} {Target.LastName}!";
}

Regeneration

Each time you save, the generated code updates automatically, reflecting attribute or signature changes without altering your custom logic.

Advanced Customization

Templates for code generation (.sbncs files) appear in the CodeGenTemplates folder. Customize these templates to fit advanced scenarios as needed.

Deploying and Further Reading

After implementation, deploy your plugin via Register Plugin(s).

[!NOTE] Assign plugins to specific solutions with [Solution("Your-Solution-Unique-Name")]. Refer to Solution-Aware-Plugins

Summary of Benefits

  • Automated registration
  • Metadata fetching
  • Eliminates manual casting and attribute parsing

Read Next: