EPiServer 7, compare new property values ​​with previous values ​​when publishing a page?

I subscribe to the DataFactory PublishingPage event using the initialization module:

DataFactory.Instance.PublishingPage += Instance_PublishingPage; void Instance_PublishingPage(object sender, PageEventArgs e) { } 

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

+4
source share
1 answer

This has recently been resolved in EPiServer 8:

In the publication event (in your example) that occurs before the page is published, it should work fine only with the ContentLoader service for comparison. ContentLoader will automatically download 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. Used only for pages that already exist, IE. new pages will have an empty ContentLink (ContentReference.Empty) within the argument.

Regarding the PublisherPage event that occurs after the page is published. You can use the following snippet to view 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 account for localization.

+2
source

Source: https://habr.com/ru/post/1493614/


All Articles