Follow @Lusse3 (136 followers)

Flickr Recent Photos

Laddning inför knatteliganKräftskivan i full rulle :-)Inför Kräftskiva :-)Ett av många äpplen från vårt äppelträd :-)Fika dags :-)Liams första fotbollsmatch. :-)Liams första tunnelbana åk. Självklart smockfullt. :-)Otrolig trevlig kväll med @plingplongen och hanna. Fast här ser vi lite seriösa ut. :-)Glömt att lägga upp bild på våran fina framsida. :-)Liam kör och ilia övervakar. :-)SoooommarMums!! Arkham expansion. :-)

Categories

How to prevent EPiServer to inherit categories to childrenpages

One of the pages I’ve been working on uses EPiServers categories a lot. But the annoying part with EPiServers categories is the way they inherit down to all children, which leads to the problem that categories can be deleted without someone meaning too. e.g you create a page called “Books” and you have a control that creates a category using the pagename. Below “Books” you’ll add all the books you want, but all those books will get the category “books” aswell. But to prevent dead categories, you might want to make a check that once you delete a page, you’ll also delete the category. Then, when deleting a childpage, you’ll also delete the real category, since the childpage also had this category. There are of course ways to prevent this but this can easily be forgotten and cause future problems. It will also mess up search results that uses categories to find information and so on.

Enough with examples, let’s get on the easy solution.


EPiServer.DataFactory.Instance.PublishingPage += CreateCategory;

EPiServer.DataFactory.Instance.SavedPage += CreateCategory;

EPiServer.DataFactory.Instance.SavingPage += CreateCategory;

EPiServer.DataFactory.Instance.CreatingPage += CreateCategory;

static void CreateCategory(object sender, EPiServer.PageEventArgs e)

{

if (!ValidPage(e.Page.PageTypeID))

{

e.Page.Property["PageCategory"].Value = string.Empty;

}

}

Basicly, you’ll call the method “CreateCategory” whenever you create, save or publish a page. Then use a method to determinate if the pagetypeid of the page you are creating is a validpage (i.e you don’t want these pages to inherit categories) and then just give it a string.Emtpy as PageCategory value.  Whatever the situation, just make a method to check if you want the page to inherit the category or not, then use the code above.

Hope this helps

EPiServer.DataFactory.Instance.PublishingPage += Instance_SavingPage;
EPiServer.DataFactory.Instance.SavedPage += Instance_SavingPage;
EPiServer.DataFactory.Instance.SavingPage += Instance_SavingPage;
EPiServer.DataFactory.Instance.CreatingPage += Instance_SavingPage;

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Additional comments powered by BackType