Events

Event definitions are managed by the Sitecore.Marketing.Definitions.Events.EventDefinitionManager class.

Accessing the EventDefinitionManager

The EventDefinitionManager is available from the Sitecore DI container. It is preferable to include a parameter of type DefinitionManagerBase<IEventDefinition, EventDefinitionRecord> in the constructor of your class and pull your class from the container, allowing the container to resolve the instance for you:

public MyClass(DefinitionManagerBase<IEventDefinition, EventDefinitionRecord> eventDefinitionManager)
{
        ...
}

If you cannot use the container to construct your class, you can use the service locator. This class is also available in the Sitecore DI container:

using Sitecore.DependencyInjection;
using Sitecore.Marketing.Definitions;

ServiceLocator.ServiceProvider.GetDefinitionManagerFactory().GetDefinitionManager<IEventDefinition>();

Defining an event

An event is defined using types from the Sitecore.Marketing.Definitions.Events.Model namespace.

Guid eventId = Guid.NewGuid(); // Event ID
CultureInfo eventCulture = new CultureInfo("es"); // Event culture
string eventName = "Newly created event"; // Event name
DateTime creationDate = DateTime.UtcNow; // Event creation date
string createdBy = "sitecore\admin"; // Event creator

EventDefinition event = new EventDefinition(eventId, "Event item name", eventCulture, eventName, creationDate, createdBy);

event.ShowInXfileAsLatestEvent = true;
event.ShowInXfileEventsList = true;
event.EngagementValuePoints = 20;

    event.IsLiveEvent = false;

Live events

The IsLiveEvent property is part of the base EventDefinition class and is used by Marketing Automation to determine if an event should be processed immediately rather than on session end. Read more about live event processing.

Saving an event

Once you have defined an event you may save it by calling the SaveAsync() method on the definition manager.

Guid eventId = Guid.NewGuid(); // Event ID
CultureInfo eventCulture = new CultureInfo("es"); // Event culture
string eventName = "Download Brochure"; // Event name
DateTime creationDate = DateTime.UtcNow; // Event creation date
string createdBy = "sitecore\admin"; // Event creator

EventDefinition event = new EventDefinition(eventId, "Event item name", eventCulture, eventName, creationDate, createdBy);

    EventDefinitionManager manager = ServiceLocator.ServiceProvider.GetDefinitionManagerFactory().GetDefinitionManager<IEventDefinition>();
    manager.SaveAsync(event);

You can also optionally activate the event during save by passing true to the second parameter of the SaveAsync() method:

manager.SaveAsync(event, true);

Activating an event

Events must be activated before they are available for use outside of management. Events can be activated when they’re saved by passing true to the activate (second) parameter of the SaveAsync() method:

manager.SaveAsync(event, true);

Events can also be activated without calling save, using the ActivateAsync() method:

manager.ActivateAsync(eventId);

The ActivateAsync() method takes the ID of the event and does not require the event definition model.

Deleting an event

To delete an event, use the Delete() method on the manager. Individual cultures cannot be deleted from the definition, only the entire definition. The culture provided to the method call should be either null (default value) or CultureInfo.InvariantCulture.

manager.Delete(eventId);

Retrieve an event

A single event can be retrieved by it’s ID using one of the Get() methods on the manager.

Guid eventId = Guid.NewGuid(); // Ensure this is the ID of an existing event

// Get by ID and culture. Will get the latest active version
IEventDefinition event = manager.Get(eventId, new CultureInfo("da"));

// Get by ID and culture. Will get the latest version, including if the version is inactive
IEventDefinition event = manager.Get(eventId, new CultureInfo("da"), true);

Retrieve an event by its alias

You can also retrieve an event by its alias:

CultureInfo eventCulture = new CultureInfo("fr-fr");
var eventDefinitionByAlias = definitionManager.GetByAlias("My alias", eventCulture);

Update an existing event

To update an existing event definition, retrieve the event by ID, edit the event

Guid eventId = Guid.NewGuid(); // Ensure this is the ID of an existing event

// Get by ID and culture. Will get the latest active version
IEventDefinition event = manager.Get(eventId, new CultureInfo("da"));

// Make updates to event
event.Name = "Updated name";

// Save to update
manager.SaveAsync(event);

Retrieving all events

The GetAll() method can be used to get all events from the manager. As there may be a large number of definitions, this method supports paging. The return value is a single page of results.

    // Get All with defaults which will be first page, page size 20, latest active versions only
    ResultSet<DefinitionResult<IEventDefinition>> events = manager.GetAll(new CultureInfo("da"), new RetrievalParameters<IEventDefinition, string>());

    // Get page 2
    ResultSet<DefinitionResult<IEventDefinition>> page2Events = manager.GetAll(new CultureInfo("da"), new RetrievalParameters<IEventDefinition, string>(pageNumber: 2));

    // Include inactive versions
ResultSet<DefinitionResult<IEventDefinition>> page1Events = manager.GetAll(new CultureInfo("da"), new RetrievalParameters<IEventDefinition, string>(), true);

To access the results, use the DataPage property of the result.

IEventDefinition event = page1Events.DataPage.ElementAt(0);

The result also includes properties to expose the total number of definitions and the current page index and page size.

long totalDefinitionCount = page1Events.Total;
int pageNumber = page1Events.PageNumber;
int pageSize = page1Events.Count;