Skip to main content

Access checkbox fields

Abstract

How to access checkbox fields using the Sitecore.Data.Fields.CheckboxField class.

You can use the Sitecore.Data.Fields.CheckboxField class to access data template fields of type Checkbox. To determine if the user has selected a Checkbox, access the Sitecore.Data.Fields.CheckboxField.Checked property. For example, to determine if the Checkbox field named CheckboxField is selected in the /Sitecore/Content/Home item in the Master database:

Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

Sitecore.Data.Items.Item home = master.GetItem("/sitecore/content/home");

Sitecore.Data.Fields.CheckboxField checkboxField = home.Fields["checkboxfield"];

if (checkboxField != null && checkboxField.Checked)
{
    // Process checked checkbox
}

To select a Checkbox field, set the Sitecore.Data.Fields.CheckboxField.Checked property. For example, to ensure the Checkbox field named CheckboxField is selected in the /Sitecore/Content/Home item in the Master database:

Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

Sitecore.Data.Items.Item home = master.GetItem("/sitecore/content/home");

Sitecore.Data.Fields.CheckboxField checkboxField = home.Fields["checkboxfield"];

if (checkboxField != null && ! checkboxField.Checked )
{
    home.Editing.BeginEdit();
    checkboxField.Checked = true;
    home.Editing.EndEdit();
}