Category selection for multiple sites in EPiServer enterprise solutions

Mattias Olsson 2015-06-16 06:46:43

In this example I'm matching the entry category name for each site with the site definition name in EPiServer. Here are the site definitions for this example. Notice the name of the sites: 

This is an example of the category structure for my two sites. Notice how the root category name match the name of the site in the screenshot above:

Editor descriptor code (EPiServer 8):

[EditorDescriptorRegistration(TargetType = typeof(CategoryList), EditorDescriptorBehavior = EditorDescriptorBehavior.OverrideDefault | EditorDescriptorBehavior.Default)]
public class CustomCategoryListEditorDescriptor : 
EPiServer.Cms.Shell.UI.ObjectEditing.EditorDescriptors.CategoryListEditorDescriptor
{
    private readonly SiteDefinitionRepository _siteDefinitionRepository;
    private readonly CategoryRepository _categoryRepository;

    public CustomCategoryListEditorDescriptor(SiteDefinitionRepository siteDefinitionRepository,           
                                              CategoryRepository categoryRepository)
    {
        if (siteDefinitionRepository == null) 
            throw new ArgumentNullException("siteDefinitionRepository");
        if (categoryRepository == null) 
            throw new ArgumentNullException("categoryRepository");
        _siteDefinitionRepository = siteDefinitionRepository;
        _categoryRepository = categoryRepository;
    }

    public override void ModifyMetadata(ExtendedMetadata metadata, 
                                        IEnumerable<Attribute> attributes)
    {
        base.ModifyMetadata(metadata, attributes);

        // Only do this if we have more than 1 site.
        if (_siteDefinitionRepository.List().Count() > 1)
        {
            var siteRootCategory = _categoryRepository
               .GetRoot()
               .Categories
               .FirstOrDefault(x => x.Name.Equals(SiteDefinition.Current.Name));

            if (siteRootCategory != null)
            {
                metadata.EditorConfiguration["root"] = siteRootCategory.ID;
            }
        }
    }
}


Editor descriptor code (EPiServer 7.5):

[EditorDescriptorRegistration(TargetType = typeof(CategoryList), EditorDescriptorBehavior = EditorDescriptorBehavior.OverrideDefault | EditorDescriptorBehavior.Default)]
public class CustomCategoryListEditorDescriptor : 
EPiServer.Cms.Shell.UI.ObjectEditing.EditorDescriptors.CategoryListEditorDescriptor
{
    private readonly SiteDefinitionRepository _siteDefinitionRepository;

    public CustomCategoryListEditorDescriptor(SiteDefinitionRepository siteDefinitionRepository)
    {
        if (siteDefinitionRepository == null) 
            throw new ArgumentNullException("siteDefinitionRepository");
        _siteDefinitionRepository = siteDefinitionRepository;
    }

    public override void ModifyMetadata(ExtendedMetadata metadata, 
                                        IEnumerable<Attribute> attributes)
    {
        base.ModifyMetadata(metadata, attributes);

        // Only do this if we have more than 1 site.
        if (_siteDefinitionRepository.List().Count() > 1)
        {
            var siteRootCategory = EPiServer.DataAbstraction.Category
               .GetRoot()
               .Categories
               .FirstOrDefault(x => x.Name.Equals(SiteDefinition.Current.Name));

            if (siteRootCategory != null)
            {
                metadata.EditorConfiguration["root"] = siteRootCategory.ID;
            }
        }
    }
}


The above code results in a filtered category selection tree for Site1: