---
title: Working with form data using the API
related:
  - https://docs.kentico.com/k9/managing-website-content/forms.md
  - https://docs.kentico.com/k9/managing-website-content/forms/managing-form-data.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).

You can use the Kentico API to manage the data that users submit through [forms](https://docs.kentico.com/k9/managing-website-content/forms.md). Two different approaches are available:

- [Work with existing form data](#working-with-existing-form-records) - load or modify the data stored for any form.
- [Handle form actions directly when they occur](#handling-form-events) - use event handlers to execute code before or after form actions occur in the system (for example whenever users submit records for a form).

## Working with existing form records

Use the following approach to access the data of a form at any time:

1. Get a **BizFormInfo** object representing the form itself (call the _BizFormInfoProvider.GetBizFormInfo_ method).
2. Get the form's class name (use the _DataClassInfoProvider.GetDataClassInfo_ method with the form's class ID as the parameter, access the _ClassName_ property of the _DataClassInfo_ object).
3. Call one of the following methods to get the form's data:
   - **BizFormItemProvider.GetItems(class name)** - loads all of the given form's records (or a filtered subset). You can loop through the _BizFormItem_ objects in the data.
   - **BizFormItemProvider.GetItem(item ID, class name)** - gets one _BizFormItem_ record specified by ID.

To get the values of specific fields from _BizFormItem_ objects, call the _GetValue_ methods — either the general _GetValue,_ which returns an object, or methods for specific data types, such as _GetStringValue._

To set values for fields, use the _SetValue_ method for individual fields and then call _SubmitChanges_ for the _BizFormItem_ object to save the results.

```csharp title="Example"

using CMS.OnlineForms;
using CMS.DataEngine;
using CMS.SiteProvider;
using CMS.Helpers;

...

// Gets the form info object for the 'ContactUs' form
BizFormInfo formObject = BizFormInfoProvider.GetBizFormInfo("ContactUs", SiteContext.CurrentSiteID);

// Gets the class name of the 'ContactUs' form
DataClassInfo formClass = DataClassInfoProvider.GetDataClassInfo(formObject.FormClassID);
string className = formClass.ClassName;

// Loads the form's data
ObjectQuery<BizFormItem> data = BizFormItemProvider.GetItems(className);

// Checks whether the form contains any records
if (!DataHelper.DataSourceIsEmpty(data))
{
    // Loops through the form's data records
    foreach (BizFormItem item in data)
    {
        string firstNameFieldValue = item.GetStringValue("FirstName", "");
        string lastNameFieldValue = item.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;

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

```

## Handling form events

To perform custom actions directly when form actions occur, [implement handlers](https://docs.kentico.com/k9/custom-development/handling-global-events.md) for [BizFormItemEvents events](https://docs.kentico.com/k9/custom-development/handling-global-events/reference-global-system-events.md#bizformitemevents).

Register the event handler at the beginning of the application's life cycle. Choose one of the following options:

- During the initialization process of the application itself — use the **CMSModuleLoader** partial class in the **App\_Code** folder.
- When initializing [custom modules](https://docs.kentico.com/k9/custom-development/creating-custom-modules/initializing-modules-to-run-custom-code.md) — override the **OnInit** method of the module class.

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 CMS.Base;
using CMS.OnlineForms;

/// <summary>
/// Partial class that allows you to register custom handler methods and classes.
/// Adds the CustomFormHandlers attribute to the partial class.
/// </summary>
[CustomFormHandlers]
public partial class CMSModuleLoader
{
    /// <summary>
    /// Custom attribute class.
    /// </summary>
    private class CustomFormHandlers: CMSLoaderAttribute
    {
        /// <summary>
        /// Called automatically when the application starts
        /// </summary>
        public override void Init()
        {
            // 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;
        }

        /// <summary>
        /// Handles the form data when users create new records for the 'ContactUs' form
        /// </summary>
        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 == "bizform.contactus")
            {
                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;

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

```
