Set Checkbox

Set Checkbox

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Configuration.Factory
  • Sitecore.Data.Database
  • Sitecore.Data.Fields.CheckboxField
  • Sitecore.Data.Items.Item
  • Sitecore.MainUtil
  • Sitecore.SecurityModel.SecurityDisabler

Set Checkbox C# Web Control Code

Sitecore.Data.Database db = Sitecore.Configuration.Factory.GetDatabase("master");
Sitecore.Data.Items.Item item = db.GetItem("/sitecore/content/TIC-Meta/FieldsDemos/Simple Fields");
if (item == null) return;

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("{0}", "C# Web Control Real-time Output");
output.RenderEndTag();

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
//using : for colon and   for space
output.Write("{0}: {1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

using (new Sitecore.SecurityModel.SecurityDisabler())
{
    try
    {
        Sitecore.Data.Fields.CheckboxField checkbox = item.Fields["example checkbox"];
        if (checkbox != null) 
        {
if (!checkbox.Checked)
{
    item.Editing.BeginEdit();
    checkbox.Checked = true;
    item.Editing.EndEdit();
}
string check = checkbox.Checked.ToString();
if (!string.IsNullOrEmpty(check))
{

//display field value and add linebreak 
output.Write(checkbox.InnerField.DisplayName + " Field Value: <b>" + checkbox.Value + "</b><br/>"); 
if (checkbox.Checked) 
output.Write("Checkbox on Simple Fields page is checked"); 
else 
output.Write("Checkbox on Simple Fields page is not checked"); 
}}
        }
    }
    finally
    {
        output.RenderEndTag(); //Div
    }
}


Set Checkbox C# Web Control Output

Example Checkbox Field Value: 1
Checkbox on Simple Fields page is checked

Notes

  • A Checkbox field stores the value 1 when a user checks the checkbox
  • Check for a value of “1” to determine if the checkbox is checked
  • The Sitecore.Configuration.Factory class encapsulates the general information available in the web.config file and contains methods that provide access to this information and the means to override it
  • Create an object for the database where the item is located - replace SitecoreDB with the name of the Sitecore database where you want to perform the update, typically “master”
  • Create an object for the target item in the database- replace itempath with the relative path to the item in the Content Tree, such as “home/about/item1” or start the path with a backslash (/) to indicate an absolute path, such as “/sitecore/content/home/about/item1”– a null value will be set in either case if the item does not exist - Sitecore does compare case when evaluating item paths
  • Create an object of type checkboxfield for the checkbox field- replace fieldname with the name of the checkboxfield for the item
  • Verify the checkbox exists and is not checked
  • Open the edit state for the item
  • Set the Checked property of the checkbox object to true
  • Close the edit state for the item

Sitecore.Data.Database db = Sitecore.Configuration.Factory.GetDatabase("SitecoreDB"); Sitecore.Data.Items.Item item = db.GetItem("itempath"); Sitecore.Data.Fields.CheckboxField checkbox = item.Fields["fieldname"]; if (checkbox != null && ! checkbox.Checked ) {       using (new Sitecore.SecurityModel.SecurityDisabler())       {           item. ItemEditing.BeginEdit();          //alternative to checkbox.Value = “1″ or item.Fields["checkboxFieldName"].Value = “1″;            checkbox.Checked = true;                    item. ItemEditing.EndEdit();              } }