Developers

Writing plugins

Write, build and install a plugin with the OmniCore.PlugIn SDK.

A plugin is a .NET class library that references the OmniCore.PlugIn package. Building it produces an .opl file, which a System Admin uploads to the server. Read Plugins for when and how plugins run.

Get the SDK

The package is on nuget.org as OmniCore.PlugIn. Every server also serves its own copy: GET /api/plugins/sdk (System Admin), Download SDK… in OmniCore Admin, or occtl plugin sdk.

The package is versioned 1.<minor>.0, but its assembly version stays 1.0.0.0, so a plugin built against any 1.x package loads on any 1.x server.

A first plugin

xml
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <Version>1.0.0</Version>
    <OmniCorePluginName>Onboarding</OmniCorePluginName>
    <OmniCorePluginDescription>Gives new staff a department</OmniCorePluginDescription>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="OmniCore.PlugIn" Version="1.0.0" />
  </ItemGroup>
</Project>
csharp
using OmniCore.PlugIn;

[FireOnAdd("Staff")]
public sealed class AssignDepartment : IPlugin
{
    public async Task ExecuteAsync(PluginEvent e, IPluginContext context, CancellationToken cancellationToken)
    {
        if (e.NewValue("department") is null)
            await context.UpdateAsync(e.EntityId, new Dictionary<string, object?> { ["department"] = "Onboarding" }, cancellationToken);
        context.Log(quot;{e.After["firstName"]} joined");
    }
}

dotnet build prints … -> …\bin\Debug\net10.0\<id>.opl (OmniCore plugin). Upload that file:

powershell
occtl plugin install .\bin\Debug\net10.0\Onboarding.opl

Triggers

Put one or more trigger attributes on a public class that implements IPlugin and has a public parameterless constructor. Each names a perspective by system name or id.

AttributeFires whenOperation
[FireOnAdd("Staff")]The perspective is added to an entity, including an entity created with it.Added
[FireOnUpdate("Staff")]Any field the perspective lists changes, on an entity that keeps it.Updated
[FireOnFieldUpdate("Staff", "department", "manager")]One of the named fields changes.FieldUpdated
[FireOnDeleted("Staff")]The perspective is removed, or the entity is deleted.Deleted
  • Only the named perspective counts. A Staff plugin doesn't fire when only fields Staff doesn't have change.
  • Shared fields fire every perspective that has them.
  • When one change matches several triggers of a class for the same perspective, it gets one event: Added, then Deleted, then FieldUpdated, then Updated.
  • Removing an entity's last perspective deletes it: its Deleted events have EntityDeleted = true and an empty After.

The event

csharp
public sealed record PluginEvent(
    PluginOperation Operation,     // Added, Updated, FieldUpdated, Deleted
    Guid EntityId,
    string Perspective,
    Guid PerspectiveId,
    IReadOnlyDictionary<string, object?> Before,
    IReadOnlyDictionary<string, object?> After,
    IReadOnlyList<string> ChangedFields,
    bool EntityDeleted,
    string? ChangedBy,             // the caller's subject, or "plugin:<id>"
    DateTimeOffset ChangedAt,
    int Depth);                    // 0 for changes made through the API, +1 per plugin in a chain

Before and After hold only the perspective's fields, as typed values: string, long, decimal, bool, DateTimeOffset, Guid or JsonNode. Missing fields are absent; e.NewValue("x") and e.OldValue("x") return null.

The context

IPluginContext reads and writes the database with full access:

Member
GetAsync(id)An entity (Id, Version, Perspectives, Fields), or null.
QueryAsync(perspective, where, take)Entities with a perspective, optionally filtered with the query language.
CreateAsync(perspectives, fields)Creates an entity.
UpdateAsync(id, fields)Sets fields; null removes one.
AddPerspectiveAsync(id, perspective, fields)Adds a perspective, with fields in the same change.
RemovePerspectiveAsync(id, perspective)Removes one; false if that deleted the entity.
DeleteAsync(id)Deletes an entity.
Log(message)Writes to the server log as [plugin <id>] message.

Writes are checked like any other (schema, required fields, relationships). A failed check throws, which counts as a failure of the event. Writes fire plugins in turn, one level deeper, up to five levels.

Running

  • One instance of each class handles every event, so keep no per-event state in fields.
  • Each event has 60 seconds (the cancellation token is cancelled then). Exceptions and timeouts are retried twice, then recorded in the plugin's recent errors.
  • Events waiting to run are lost if the server stops.

The manifest

The .opl is a zip with plugin.json and the plugin's DLLs (with its NuGet dependencies; OmniCore.PlugIn.dll is left out, as the server provides it). Without a plugin.json in the project, one is made from these properties:

PropertyDefault
OmniCorePluginIdThe assembly name. Uploading the same id again replaces the plugin.
OmniCorePluginNameThe id
OmniCorePluginVersion$(Version), else 1.0.0
OmniCorePluginDescription$(Description)
OmniCorePluginPackagefalse skips building the .opl

Adding schema

A hand-written plugin.json can add schema when the plugin is installed:

json
{
  "id": "Acme.Suppliers",
  "name": "Supplier sync",
  "version": "2.1.0",
  "assemblies": ["Acme.Suppliers.dll"],
  "schema": {
    "fields": [
      { "systemName": "vatNumber", "displayName": "VAT Number", "type": "Text" },
      { "systemName": "buyer", "displayName": "Buyer", "type": "Reference" }
    ],
    "perspectives": [
      { "name": "Supplier", "displayName": "Supplier", "fields": [ { "field": "vatNumber", "required": true }, { "field": "buyer" } ] },
      { "name": "Contact", "fields": [ { "field": "vatNumber" } ] }
    ],
    "relationships": [
      { "name": "SupplierBuyer", "displayName": "Buyer",
        "from": { "perspective": "Supplier", "multiplicity": "Many", "field": "buyer" },
        "to": { "perspective": "Staff", "multiplicity": "One" } }
    ]
  }
}
  • Fields are created if missing; an existing field must have the same type.
  • Perspectives are created if missing; an existing one (here Contact) gains the fields it lacks.
  • Relationships are created if missing; the data must satisfy them, and an existing one must be identical.

Installing is all or nothing: if anything conflicts, the upload is refused with every problem listed, and nothing changes. Removing a plugin leaves its schema.

Managing plugins

occtlAPI
Install or replaceocctl plugin install x.oplPOST /api/plugins
List, inspectocctl plugin list, occtl plugin get <id>GET /api/plugins, GET /api/plugins/{id}
Turn off and onocctl plugin disable <id>, enable <id>POST /api/plugins/{id}/disable, /enable
Removeocctl plugin remove <id>DELETE /api/plugins/{id}

OmniCore Admin's Plugins tab does the same, and shows each plugin's classes, triggers, activity and recent errors.