AI OnAI Off
Hi!
You can create a custom attribute and replace the built in EditorDescriptor for the CategoryList type. Here's a sample:
Custom attribute:
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] public class CategorySelectionAttribute : Attribute { public bool Multiple { get; set; } public int RootCategoryId { get; set; } public string RootCategoryName { get; set; } public CategorySelectionAttribute() { Multiple = true; } public int GetRootCategoryId() { if (RootCategoryId > 0) { return RootCategoryId; } if (!string.IsNullOrEmpty(RootCategoryName)) { var category = Category.Find(RootCategoryName); if (category != null) { return category.ID; } } return Category.GetRoot().ID; } }
Editor descriptor:
[EditorDescriptorRegistration(TargetType = typeof(CategoryList), EditorDescriptorBehavior = EditorDescriptorBehavior.OverrideDefault)] public class CustomCategoryListEditorDescriptor : EPiServer.Cms.Shell.UI.ObjectEditing.EditorDescriptors.CategoryListEditorDescriptor { public override void ModifyMetadata(ExtendedMetadata metadata, IEnumerable<Attribute> attributes) { base.ModifyMetadata(metadata, attributes); var categorySelectionAttribute = attributes.OfType<CategorySelectionAttribute>().FirstOrDefault(); if (categorySelectionAttribute != null) { metadata.EditorConfiguration["multiple"] = categorySelectionAttribute.Multiple; metadata.EditorConfiguration["root"] = categorySelectionAttribute.GetRootCategoryId(); } } }
Then you can decorate your content type property like this:
[CategorySelection(RootCategoryId = 123)] public virtual CategoryList MyCategories { get; set; }
Or like this:
[CategorySelection(RootCategoryName = "MyCategoryName")] public virtual CategoryList MyCategories { get; set; }
Hi!
Upgrade from cms 6r2 to 7.9.2
Im actually looking for a category picker that gets child categories from a specific Category node?
Needs to be a multi select. Listing for example News Categories or Image Categories and so on...
Its helping the editors since we do have a lot of Categories.
When Im using a legacy Category Picker. It does not work when a new page is created.
Throws a [JsonSerializationException: Unexpected initial token 'Null' when populating object. Expected JSON object or array. Path '', line 1, position 4.]
[BackingType(typeof(CategoryPicker))]
[UIHint(EPiServer.Web.UIHint.Legacy)]