Virtual Happy Hour this month, Jun 28, we'll be getting a sneak preview at our soon to launch SaaS CMS!

Try our conversational search powered by Generative AI!

Does EpiServer provide any events (adding, editing, deleting) for categories?

Vote:
 

Hello,

I'm wondering can we listen to any events for categories? More specifically, I need to do some action when a content editor adds a new category.

If this functionality is not available yet, it should be proposed as a new feature frown

Any other opinion how to solve this requirement?

Thanks

#187962
Feb 07, 2018 13:10
Vote:
 

One trick would be to decorate `EPiServer.DataAbstraction.CategoryRepository` class in StructureMap, intercept method invocations and call something that you need then.

Not sure (can't remember) if you can decorate abstract class. If that's not an option in structure map (which sounds logical from OOP perspective) that option maybe would be to create an interceptor around it.

Agree that category repo should be hidden behind interface and operations should provide "hookable" events tho..

#187981
Feb 07, 2018 21:08
Vote:
 

Thank you Valdis,

I will try your suggestion.

#188009
Feb 08, 2018 11:04
Vote:
 

I managed to accomplish this requirement with the decorator, I couldn't use interceptor because only virtual members of the class can be intercepted.

abstract class CategoryRepositoryDecorator : CategoryRepository
    { 
        private readonly CategoryRepository _inner; 

        public CategoryRepositoryDecorator(CategoryRepository inner) 
        {
            _inner = inner; 
        }

        public override void Save(Category category)
        {
            _inner?.Save(category);
        }

        public override void Delete(Category category)
        {
            _inner?.Delete(category);
        }

        public override Category GetRoot()
        {
            return _inner?.GetRoot();
        }

        public override Category Get(int id)
        {
            return _inner?.Get(id);
        }

        public override Category Get(string name)
        {
            return _inner?.Get(name);
        }
    }

class DecoratedCategoryRepository : CategoryRepositoryDecorator
    {
        public DecoratedCategoryRepository(CategoryRepository inner) : base(inner)
        {
        }

        public override void Save(Category category)
        {
            base.Save(category);
            Debug.WriteLine("Overriden category save method triggered!");
        }

        public override void Delete(Category category)
        {
            base.Delete(category);
            Debug.WriteLine("Overriden delete category method triggered!");
        }
    }

        private static void ConfigureContainer(ConfigurationExpression container)
        {
            container.For<CategoryRepository>().DecorateAllWith<DecoratedCategoryRepository>();
        }

Thanks

#188285
Edited, Feb 16, 2018 20:27
Vote:
 

This sounds like great sccess! ;)

Nothing, just 2 my cents, usually I name decorated object as "_inner". For me at least makes sense when reading code - this is something being wrapped.

#188286
Feb 16, 2018 20:38
Vote:
 

Accepted wink

#188287
Feb 16, 2018 20:48
Vote:
 

Looks like this was added in the latest update (update 206) with the new interface ICategoryEvents

https://world.episerver.com/releases/episerver---update-206/

#189618
Mar 21, 2018 13:48
Vote:
 

Thanks for the info wink

#189624
Mar 21, 2018 14:00
Vote:
 

Version 11.5.0 added events for categories, see https://world.episerver.com/documentation/Release-Notes/?versionFilter=11.5.0&packageFilter=EPiServer.CMS.Core&typeFilter=All

#189704
Mar 23, 2018 8:38
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.