---
title: Handle form events
related:
  - https://docs.kentico.com/documentation/developers-and-admins/customization/handle-global-events/reference-global-system-events.md
  - https://docs.kentico.com/documentation/developers-and-admins/customization/handle-global-events/handle-content-events.md
  - https://docs.kentico.com/documentation/developers-and-admins/customization/extend-the-administration-interface/ui-pages/reference-ui-page-templates/listing-ui-page-template/export-listing-data.md
  - https://docs.kentico.com/documentation/developers-and-admins/development/builders/form-builder/form-components.md
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

Xperience raises events when visitors submit form data and when administration users export form submissions. Handle these events to run custom logic, for example to integrate submissions with an external system or to keep an audit record of exports.

## Handle form record events

You can run custom actions directly when form actions occur by [implementing handlers](https://docs.kentico.com/documentation/developers-and-admins/customization/handle-global-events.md) for events of the `BizFormItemEvents` type. The system invokes the following events in response to form actions:

- `Update` – invoked when a form record is updated.
- `Insert` – invoked on each form submission.
- `Delete` – invoked when a form record is deleted.

Use the appropriate event type to perform your custom actions or data modifications `Before` or `After` the given form event.

To access the data of the active form in the event handler, use the `Item` property (`BizFormItem` type) of the `BizFormItemEventArgs` parameter. The parameter is available for all form item event handlers.

```csharp title="Example"
using System;

using CMS;
using CMS.DataEngine;
using CMS.OnlineForms;

// Registers the custom module into the system
[assembly: RegisterModule(typeof(FormHandlerModule))]

public class FormHandlerModule : Module
{
    // Module class constructor, the system registers the module under the name "CustomFormHandlers"
    public FormHandlerModule()
        : base("CustomFormHandlers")
    {
    }

    // Contains initialization code that is executed when the application starts
    protected override void OnInit()
    {
        base.OnInit();

        // Assigns a handler to the BizFormItemEvents.Insert.After event
        // This event occurs after the creation of every new form record
        BizFormItemEvents.Insert.After += FormItem_InsertAfterHandler;
    }

    // Handles the form data when users create new records for the 'ContactUs' form
    private void FormItem_InsertAfterHandler(object sender, BizFormItemEventArgs e)
    {
        // Gets the form data object from the event handler parameter
        BizFormItem formDataItem = e.Item;

        // Checks that the form record was successfully created
        // Ensures that the custom actions only occur for records of the 'ContactUs' form
        // The values of form class names must be in lower case
        if (formDataItem != null && formDataItem.BizFormClassName.Equals("bizform.contactus", StringComparison.OrdinalIgnoreCase))
        {
            string firstNameFieldValue = formDataItem.GetStringValue("FirstName", "");
            string lastNameFieldValue = formDataItem.GetStringValue("LastName", "");

            // Perform any required logic with the form field values

            // Variable representing a custom value that you want to save into the form data
            object customFieldValue = "customValue";

            // Sets and saves a value for the form record's 'CustomField' field
            formDataItem.SetValue("CustomField", customFieldValue);
            formDataItem.SubmitChanges(false);
        }
    }
}
```

## Handle form data export events

[Exporting form submissions](https://docs.kentico.com/documentation/business-users/digital-marketing/forms/manage-form-submissions.md#export-form-submissions) raises the `AfterExportListingEvent` event. Handle the event to keep an audit record of who exported form submissions and when.

The event is not specific to forms, every administration listing page that has the [export enabled](https://docs.kentico.com/documentation/developers-and-admins/customization/extend-the-administration-interface/ui-pages/reference-ui-page-templates/listing-ui-page-template/export-listing-data.md#enable-data-export) raises it, including custom listing pages inheriting directly or indirectly from `ListingPage<TClientProperties>`. To react only to exports performed in the Forms application, compare the `ListingPageTypeName` property of the event data against the full name of the `FormSubmissionsTab` type (available in the `Kentico.Xperience.Admin.DigitalMarketing.UIPages` namespace).

```csharp title="Handle exports of form submissions only"
public Task HandleAsync(AfterExportListingEvent asyncEvent, CancellationToken cancellationToken)
{
    // Exits for exports performed from other listing pages
    // The property carries the full type name, including the namespace
    if (asyncEvent.Data.ListingPageTypeName != typeof(FormSubmissionsTab).FullName)
    {
        return Task.CompletedTask;
    }

    // Perform any required logic for form submission exports

    return Task.CompletedTask;
}
```

The event data does not identify the individual form whose submissions were exported. See [Run custom code after an export](https://docs.kentico.com/documentation/developers-and-admins/customization/extend-the-administration-interface/ui-pages/reference-ui-page-templates/listing-ui-page-template/export-listing-data.md#run-custom-code-after-an-export) for the full reference of the event data, an example handler, and the required registration.

The event runs after the exported file is built and cannot modify its content. To control how the values of a form component appear in the export, implement a [text extractor](https://docs.kentico.com/documentation/developers-and-admins/development/builders/form-builder/form-components.md#provide-text-representations-of-component-values).
