Dec 18, 2017
(1 votes)

Comparing draft content to published content

We had a situation arise the other day where we needed to compare some values in a draft page against the currently published version. Based on some fields changing we knew we needed to reindex a list of related pages.

This all takes place in the IContentEvents PublishingContent event handler which has been wired up in an InitializationModule.

The trick here is to cast e.Content to get the current draft and to use e.ContentLink.ToReferenceWithoutVersion to get the published version so that you can compare.

Here is a code sample that checks for a change to the Name and logs it.

using System;
using System.Linq;
using EPiServer;
using EPiServer.Core;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.ServiceLocation;
using EPiServer.Logging;

public class PageDataUpdatedInitialization : IInitializableModule
    private static readonly ILogger Logger = LogManager.GetLogger();
    private Injected contentLoader;

    public void Initialize(InitializationEngine context)
        var events = context.Locate.ContentEvents();
        events.PublishingContent += this.PublishingContent;

    private void PublishingContent(object sender, ContentEventArgs e)
        var newPageData = e.Content as PageData;
        if (newPageData == null)

        var oldPageData = contentLoader.Service?.Get(e.ContentLink.ToReferenceWithoutVersion());
        if (oldPageData == null)

        // check to see whether any updates were made to the specific value
        if (!ValueChanged(oldPageData, newPageData))

        // if so, do something
        Logger.Debug("Value has changed from {0} to {1}", oldPageData.Name, newPageData.Name);

    private static bool ValueChanged(PageData oldPageData, PageData newPageData)
        var oldName = oldPageData.Name;
        var newName = newPageData.Name;

        if (newName != oldName)
            return true;

        return false;

    public void Uninitialize(InitializationEngine context)
        //Add uninitialization logic

Dec 18, 2017


valdis Dec 18, 2017 06:32 PM

just a couple of my humble cents here:

  • I would not do all the logic in initialization module. these are meant to just initialize things. rest of the logic I would move to separate service
  • there I would require IContentLoader from constructor (btw, you are missing T part of the Injected field data type) - therefore I would be required to pass it in from init module somehow
  • and also in the same way - I could require IContentEvents implementation to be supplied.
  • not a big deal - but avoids you to use anti-patterns, such as ServiceLocator.

Also I would encourage you to take a look at brilliant way to handle Episerver events differently -

KennyG Dec 18, 2017 08:24 PM

Thanks for the feedback Valdis! 

Please login to comment.
Latest blogs
Translating Optimizely CMS 12 UI components

Optimizely CMS 12 have been out for a while now, but still some elements haven't been properly translated resulting in a GUI defaulting to english....

Eric Herlitz | Jan 26, 2023 | Syndicated blog

Image preview in Optimizely CMS12 all properties view

With these simple steps, you can now see an Image and its Metadata, including size and dimensions, when editing an Image property in Optimizely...

Tomas Hensrud Gulla | Jan 26, 2023 | Syndicated blog

Setting up the ImageEditor in Optimizely CMS 12

Setting up certain configurations on Opimizely CMS 12 differs quite a bit from prior versions of (Episerver CMS 11 and older). Here's a small guide...

Eric Herlitz | Jan 25, 2023 | Syndicated blog

Happy Hour Returning in February

Hi everyone! It's been a while and we're excited to resume our Happy Hour in February for more learning, sharing, connecting, relaxing, and just to...

Patrick Lam | Jan 24, 2023