Try our conversational search powered by Generative AI!

KennyG
Dec 18, 2017
  2790
(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;


[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
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)
            return;

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

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

        // 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

Comments

valdis
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 - http://marisks.net/2017/02/12/better-event-handling-in-episerver/

KennyG
KennyG Dec 18, 2017 08:24 PM

Thanks for the feedback Valdis! 

Please login to comment.
Latest blogs
Why C# Developers Should Embrace Node.js

Explore why C# developers should embrace Node.js especially with Optimizely's SaaS CMS on the horizon. Understand the shift towards agile web...

Andy Blyth | May 2, 2024 | Syndicated blog

Is Optimizely CMS PaaS the Preferred Choice?

As always, it depends. With it's comprehensive and proven support for complex business needs across various deployment scenarios, it fits very well...

Andy Blyth | May 2, 2024 | Syndicated blog

Adding market segment for Customized Commerce 14

Since v.14 of commerce, the old solution  for adding market segment to the url is not working anymore due to techinal changes of .NET Core. There i...

Oskar Zetterberg | May 2, 2024

Blazor components in Optimizely CMS admin/edit interface

Lab: Integrating Blazor Components into Various Aspects of Optimizely CMS admin/edit interface

Ove Lartelius | May 2, 2024 | Syndicated blog

Anonymous Tracking Across Devices with Optimizely ODP

An article by Lead Integration Developer, Daniel Copping In this article, I’ll describe how you can use the Optimizely Data Platform (ODP) to...

Daniel Copping | Apr 30, 2024 | Syndicated blog

Optimizely Forms - How to add extra data automatically into submission

Some words about Optimizely Forms Optimizely Forms is a built-in add-on by Optimizely development team that enables to create forms dynamically via...

Binh Nguyen Thi | Apr 29, 2024