---
title: "Page events"
slug: "page-events"
abstract: ""
---
# Page events

Page event definitions are managed by the `Sitecore.Marketing.Definitions.PageEvents.PageEventDefinitionManager` class.

> [!note]
> Page events are specialized events. `IPageEventDefinition` inherits `IEventDefinition`.

## Accessing the PageEventDefinitionManager

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

```csharp
public MyClass(DefinitionManagerBase<IPageEventDefinition, PageEventDefinitionRecord> pageEventDefinitionManager)
{
        ...
}

```

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:

```csharp
using Sitecore.DependencyInjection;
using Sitecore.Marketing.Definitions;

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

```

## Defining a page event

A page event is defined using types from the `Sitecore.Marketing.Definitions.PageEvents.Model` namespace.

```csharp
Guid pageEventId = Guid.NewGuid(); // Page event ID
CultureInfo pageEventCulture = new CultureInfo("es"); // Page event culture
string pageEventName = "Newly created page event"; // Page event name
DateTime creationDate = DateTime.UtcNow; // Page event creation date
string createdBy = "sitecore\admin"; // Page event creator

PageEventDefinition pageEvent = new PageEventDefinition(pageEventId, "Page event item name", pageEventCulture, pageEventName, creationDate, createdBy);

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

```

### 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](https://doc.sitecore.com/xp/en/developers/103/sitecore-experience-platform/marketing-automation-plan-and-activity-enrollment.html "Marketing Automation plan and activity enrollment").

## Saving a page event

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

```csharp
Guid pageEventId = Guid.NewGuid(); // Page event ID
CultureInfo pageEventCulture = new CultureInfo("es"); // Page event culture
string pageEventName = "Newly created page event"; // Page event name
DateTime creationDate = DateTime.UtcNow; // Page event creation date
string createdBy = "sitecore\admin"; // Page event creator

PageEventDefinition pageEvent = new PageEventDefinition(pageEventId, "Page event item name", pageEventCulture, pageEventName, creationDate, createdBy);

pageEvent.AdditionalRegistrationsAreIgnored = true;
pageEvent.IsMonetaryValueApplicable = true;

    PageEventDefinitionManager manager = ServiceLocator.ServiceProvider.GetDefinitionManagerFactory().GetDefinitionManager<IPageEventDefinition>();

manager.SaveAsync(page event);

```

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

```text
manager.SaveAsync(page event, true);

```

## Activating a page event

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

```text
manager.SaveAsync(pageEvent, true);

```

Page events can also be activated without calling save, using the `ActivateAsync()` method:

```text
manager.ActivateAsync(pageEventId);

```

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

## Deleting a page event

To delete a page 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`.

```text
manager.Delete(pageEventId);

```

## Retrieve a page event

You can retrieve a single page event by its ID using one of the `Get()` methods on the manager.

```csharp
Guid pageEventId = Guid.NewGuid(); // Ensure this is the ID of an existing page event

// Get by ID and culture. Will get the latest active version
IPageEventDefinition pageEvent = manager.Get(pageEventId, new CultureInfo("da"));

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

```

### Retrieve a contact list by its alias

You can also retrieve a page event by its [alias](https://doc.sitecore.com/xp/en/developers/103/sitecore-experience-platform/definition-aliases.html "Definition aliases"):

```csharp
CultureInfo pageEventCulture = new CultureInfo("fr-fr");
var pageEventDefinitionByAlias = definitionManager.GetByAlias("My alias", pageEventCulture);

```

## Update an existing page event

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

```csharp
Guid pageEventId = Guid.NewGuid(); // Ensure this is the ID of an existing page event

// Get by ID and culture. Will get the latest active version
IPageEventDefinition pageEvent = manager.Get(pageEventId, new CultureInfo("da"));

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

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

```

## Retrieving all page events

The `GetAll()` method can be used to get all page 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<IPageEventDefinition>> pageEvents = manager.GetAll(new CultureInfo("da"), new RetrievalParameters<IPageEventDefinition, string>());

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

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

```

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

```text
IPageEventDefinition page event = page1pageEvents.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 = page1pageEvents.Total;
int pageNumber = page1pageEvents.PageNumber;
int pageSize = page1pageEvents.Count;

```