EPiServer 7, compare new property values with previous values when a page is published?

StackOverflow https://stackoverflow.com/questions/17876849

  •  04-06-2022
  •  | 
  •  

Question

I'm subscribing to the DataFactory event PublishingPage using an initialization module:

DataFactory.Instance.PublishingPage += Instance_PublishingPage;

void Instance_PublishingPage(object sender, PageEventArgs e)
{
}

The parameter PageEventArgs contains the new page that is beeing published (e.Page) Is there a way to get hold of the previous version of this page and compare its property values with the new version that is beeing published?

Was it helpful?

Solution

Recently solved this in EPiServer 8:

In the publishing event (in your example), which occurs before the page is published it should work fine just using the ContentLoader service for comparison. The ContentLoader will automatically fetch the published version.

if (e.Content != null && !ContentReference.IsNullOrEmpty(e.Content.ContentLink))
{
  var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
  var publishedVersionOfPage = contentLoader.Get<IContent>(e.Content.ContentLink);
}

Note: Only applies to pages that already exists, IE. new pages will have an empty ContentLink (ContentReference.Empty) within the e-argument.

As for the PublishedPage event, which occurs after the page is published. You can use the following snippet to get hold of the previous published version (if any):

var cvr = ServiceLocator.Current.GetInstance<IContentVersionRepository>();
IEnumerable<ContentVersion> lastTwoVersions = cvr
  .List(page.ContentLink)
  .Where(p => p.Status == VersionStatus.PreviouslyPublished || p.Status == VersionStatus.Published)
  .OrderByDescending(p => p.Saved)
  .Take(2);

if (lastTwoVersions.Count() == 2) 
{
   // lastTwoVersions now contains the two latest version for comparison
   // Or the latter one vs the e.Content object.
}

Note: this answer does not take localization in account.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top