How to select a category by default?

Vote:
 

Hi,

I want to select a certain category(news) by default when I create a new page from a specific pagetype. Much like you can do with the properties through admin-mode where you can allow it to be preset. This is so the editors can't forget to check it.

Feels like it should be fairly simple task but I can't seem to find a simple solution.

How do I do this?

Thanks!

//Nicklas

#67853
Mar 13, 2013 15:53
Vote:
 

One way to do this is by hooking up the DataFactory.Instance.CreatingPage event. I'd probably do this in Application_Start in Global to avoid having the event being subscribed to multiple times. In the event handler you can check what type of page being created and set default values.

#67861
Mar 13, 2013 16:11
Vote:
 

Ok, good idea!
So I'm hooked up on the CreatingPage event but how do I acually set the default values then? Do I treat it like a property or do I have to use the CategoryList's ResolveCategoryNames or something like that?

Thanks for you input!

#67891
Mar 14, 2013 8:39
Vote:
 

Your event handler should look something like this (I only have a CMS 7 site to look at right now but it should be similar in CMS 6):

        void Instance_CreatingPage(object sender, EPiServer.PageEventArgs e)
        {
            if (e.Page.PageTypeName == "<your value here>")
            {
                e.Page.Category.Add("<your int id here>");
            }
        }

If I where to do this I'd probably store the values (PageTypeName and Category) in a settings page so that the editor can modify them and perhaps set more then one category as default. I haven't tested the code but there shouldn't be much more to it...

 

#67893
Mar 14, 2013 9:24
Vote:
 

Thanks Henrik, that worked like a charm!

Much appreciated!

//Nicklas

#67981
Mar 14, 2013 12:29
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.