Try our conversational search powered by Generative AI!

Multiselect Category Picker for 7.5

Vote:
 

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)]

#121222
May 05, 2015 9:24
Vote:
 

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; }
#121260
Edited, May 05, 2015 21:16
Vote:
 
<p>Neat and great! thanks =)</p>
#121270
May 06, 2015 8:10
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.