November Happy Hour will be moved to Thursday December 5th.

When publishing a page, create extra subpages automatically

Vote:
 

Hi there,

I have a pagetype called Education.  Now when i create a page of that PageType and publish it, i want to auto create 3 subpages of another pagetype called EducationVariant  and have all 3 items have the pageName 'fulltime', 'parttime' and 'dual' respectively and then publish them(or save/unpublished).

how would i go about doing this in EPiServer 8 (latest updates)? 

#121091
May 01, 2015 14:16
Vote:
 

Hi Vishal!

I would create a IInitializableModule and listen to DataFactory event PublishedContent. It could look something like this:

[EPiServer.Framework.InitializableModule]
public class DataFactorySubscriber : EPiServer.Framework.IInitializableModule
{
    public void Initialize(EPiServer.Framework.Initialization.InitializationEngine context)
    {
        context.InitComplete += InitComplete;
    }

    private static void InitComplete(object sender, EventArgs e)
    {
        DataFactory.Instance.PublishedContent += PublishedContent;
    }

    private static void PublishedContent(object sender, ContentEventArgs e)
    {
        var educationPage = e.Content as Education;

        if (educationPage != null)
        {
            CreateVariantPage("fulltime", e.ContentLink);
            CreateVariantPage("parttime", e.ContentLink);
            CreateVariantPage("dual", e.ContentLink);
        }
    }

    private void CreateVariantPage(string pageName, ContentReference parentLink) 
    {
        bool alreadyExists = DataFactory.Instance.GetChildren<EducationVariant>(parentLink).Count(child => child.Name.Equals(pageName, StringComparison.InvariantCultureIgnoreCase)) > 0;

        if (alreadyExists)
        {
            return;
        }

        var variant = DataFactory.Instance.GetDefault<EducationVariant>(parentLink);

        variant.PageName = pageName;

        DataFactory.Instance.Save(variant, SaveAction.Publish, AccessLevel.NoAccess);
    }

    public void Uninitialize(EPiServer.Framework.Initialization.InitializationEngine context)
    {
        context.InitComplete -= InitComplete;
        DataFactory.Instance.PublishedContent -= PublishedContent;
    }

    public void Preload(string[] parameters)
    {
    }
}

Hope that helps!

#121093
May 01, 2015 16:47
Vote:
 

Thanks  alot for this mattias... This is what I needed... 

#121098
May 02, 2015 0:01
Vote:
 

Great, just what I was looing for!

#123319
Jul 01, 2015 14:18
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.