Last active
September 20, 2018 07:42
-
-
Save AThraen/b35920bc2a6473beebfcf9fae8a670ba to your computer and use it in GitHub Desktop.
Initialization code that ensures new blog posts are placed in a year/month hierarchy instead of a flat structure.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Linq; | |
| using EPiServer.Framework; | |
| using EPiServer.Framework.Initialization; | |
| using EPiServer.ServiceLocation; | |
| using EPiServer.Core; | |
| using AllanTech.Web.Models.Pages; | |
| using EPiServer; | |
| using AllanTech.Web.Helpers; | |
| using System.Web.Mvc; | |
| using System.Globalization; | |
| namespace AllanTech.Web.Business.Initialization | |
| { | |
| /// <summary> | |
| /// Place new blog posts the right place. | |
| /// </summary> | |
| [InitializableModule] | |
| [ModuleDependency(typeof(EPiServer.Web.InitializationModule))] | |
| public class BlogInit : IInitializableModule | |
| { | |
| private IContentRepository repo { get; set; } | |
| public void Initialize(InitializationEngine context) | |
| { | |
| //Add initialization logic, this method is called once after CMS has been initialized | |
| context.InitComplete += Context_InitComplete; | |
| } | |
| public BlogInit() | |
| { | |
| } | |
| private void Context_InitComplete(object sender, EventArgs e) | |
| { | |
| this.repo = ServiceLocator.Current.GetInstance<IContentRepository>(); | |
| var events = ServiceLocator.Current.GetInstance<IContentEvents>(); | |
| events.CreatingContent += Events_CreatingContent; | |
| } | |
| private void Events_CreatingContent(object sender, EPiServer.ContentEventArgs e) | |
| { | |
| if(e.Content is BlogPostPage) | |
| { | |
| //Check location. Create correct location and put it there. | |
| var blogroot = ContentHelpers.GetStartPage().BlogRoot; | |
| if (e.Content.ParentLink == blogroot.ToReferenceWithoutVersion()) | |
| { | |
| //Created on the blog root, place properly | |
| //Handle Year | |
| var yearpage=repo.GetChildren<ListPage>(blogroot).Where(lp => lp.Name == DateTime.Now.Year.ToString()).Select(lp => lp.ContentLink).FirstOrDefault(); | |
| if (yearpage == null) | |
| { | |
| var p=repo.GetDefault<ListPage>(blogroot); | |
| p.Name = DateTime.Now.Year.ToString(); | |
| yearpage=repo.Save(p, EPiServer.DataAccess.SaveAction.Publish); | |
| } | |
| //Handle Month | |
| string fullMonthName =DateTime.Now.ToString("MMMM", CultureInfo.InvariantCulture); | |
| var monthpage = repo.GetChildren<ListPage>(yearpage).Where(lp => lp.Name == DateTime.Now.Month.ToString()).Select(lp => lp.ContentLink).FirstOrDefault(); | |
| if (monthpage == null) | |
| { | |
| var p = repo.GetDefault<ListPage>(yearpage); | |
| p.Name = DateTime.Now.Month.ToString(); | |
| p.Title = DateTime.Now.ToString("MMMM", CultureInfo.InvariantCulture); | |
| monthpage = repo.Save(p, EPiServer.DataAccess.SaveAction.Publish); | |
| } | |
| e.Content.ParentLink = monthpage; | |
| } | |
| } | |
| } | |
| public void Uninitialize(InitializationEngine context) | |
| { | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment