Access checkbox fields

Version: 10.4

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:

RequestResponse
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:

RequestResponse
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();
}

Do you have some feedback for us?

If you have suggestions for improving this article,