Article Handling Events

An event is way to react to actions that occur in the platform. Each event executor creates a copy of the content being executed. The In-Process API is used to access the community events. To find out what events are available please see the In-Process API Services.

Event handlers should be registered within the Initialize() method of the IPlugin  interface implementation of your  . 

Why Should I Handle Events In My Plugin

By setting up an event handler your plugin can immediately react to specific events as they occur in the platform.

Platform events usually revolve around two states before and after. Creating a Before event handler will give you access to an editable event argument that will be returned prior to executing the particular event. For example on a BeforeCreate event you can remove HTML tags from a field that should be text only. These changes will then be applied to entity being created before it is committed to long-term storage.

The opposite is true for the After event handler. All changes have been processed for that event and you will receive a read-only argument. An AfterCreate event can be used for sending notifications. In this instance a Before event handler could be removing foul or abusive language from the content and will be safe to send after it is processed. This is an important distinction since the order of the registered event handlers to a single event cannot be set or determined.

Creating an Event Handler

This sample uses the Initialize method of an IPlugin implementation to register a BeforeCreated event handler for a blog post. Then the handler method then writes a message to the EventLogs.

public void Initialize()
{
    Telligent.Evolution.Extensibility.Apis.Get<Telligent.Evolution.Extensibility.Api.Version1.IBlogs>().Events.BeforeCreate += new Telligent.Evolution.Extensibility.Api.Version1.BlogBeforeCreateEventHandler(BlogEvent_BeforeCreate);
}

private void BlogEvent_BeforeCreate(Telligent.Evolution.Extensibility.Api.Version1.BlogBeforeCreateEventArgs blogBeforeCreateArgs)
{
    Telligent.Evolution.Extensibility.Apis.Get<Telligent.Evolution.Extensibility.Api.Version1.IEventLog>().Write($"A blog post with the title \"{blogBeforeCreateArgs.Name}\" is about to be created.", new Telligent.Evolution.Extensibility.Api.Version1.EventLogEntryWriteOptions
	{
		Category = "Samples"
	});
}