EPiServer custom property: Multiple Category checkbox list from specific sub category
April 1st, 2013Sometimes editors have to select multiple categories from a long list, including other category nodes which might not be relevant for that page.
This is a custom property which you can configure to only list categories from a certain category parent. The editors can then select multiple categories directly from a list of checkboxes.
Configure the category node by either:
1. Writing the name (not the description) of the category in the help text of your property
2. Naming your property exactly the same as the name (not the description) of the category
?Download PropertyCategoryCheckBoxList.cs
1 2 3 4 5 6 7 8 | [PageDefinitionTypePlugIn(DisplayName = "Category Checkbox List")] public class PropertyCategoryCheckBoxList : PropertyMultipleValue { public override EPiServer.Core.IPropertyControl CreatePropertyControl() { return new PropertyCategoryCheckBoxListControl(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | public class PropertyCategoryCheckBoxListControl : PropertySelectMultipleControlBase { protected override void SetupEditControls() { Category parentCategory = null; PageDefinition definition = PageDefinition.Load(this.PropertyData.PageDefinitionID); if (!string.IsNullOrEmpty(definition.HelpText)) parentCategory = Category.Find(definition.HelpText); if (parentCategory == null) parentCategory = Category.Find(this.Name); if (parentCategory == null) parentCategory = Category.GetRoot(); CategoryCollection categories = parentCategory.Categories; foreach (Category category in categories) { ListItem li = new ListItem(category.Description, category.ID.ToString()); li.Selected = CurrentPage.Category.Any(c => c == category.ID); this.EditControl.Items.Add(li); } } public override void ApplyEditChanges() { base.ApplyEditChanges(); PageData clone = CurrentPage.CreateWritableClone(); foreach (ListItem li in this.EditControl.Items) { if (((PropertyMultipleValue)PropertyData).IsValueActive(li.Value)) clone.Category.Add(Category.Find(Convert.ToInt32(li.Value)).ID); else clone.Category.Remove(Category.Find(Convert.ToInt32(li.Value)).ID); } if (clone.IsModified) DataFactory.Instance.Save(clone, EPiServer.DataAccess.SaveAction.Save | EPiServer.DataAccess.SaveAction.ForceCurrentVersion, EPiServer.Security.AccessLevel.NoAccess); } } |
And if you use PageTypeBuilder, here is an example of the definition:
1 2 | [PageTypeProperty(Type = typeof(PropertyCategoryCheckBoxList), EditCaption = "Kategorier", HelpText="Put_Your_Sub_Category_Name_Here_Please", SortOrder = 100)] public virtual string Categories { get; set; } |