Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • andrew shearer 510 posts 659 karma points
    Nov 06, 2016 @ 20:28
    andrew shearer
    1

    Umbraco publishing API events: knowing what changed?

    Hello

    What is the official way in the Umbraco API to determine what changed during publication of a content item from the previous time it was published? For example, my content item has a RTE body text property, and I would like to know if it changed and how (i.e. before and after state so that I can perform my own comparison on the delta/differences).

    Thanks Andrew

  • Jonathan Richards 288 posts 1742 karma points MVP
    Nov 07, 2016 @ 13:58
    Jonathan Richards
    0

    Hi

    There is a method called IsPropertyDirty() that can be called to check if a property has changed. I know of nothing that can get you the original value, but you could roll your own code to do this

    Snippet

    using Umbraco.Core;
    public class Register : ApplicationEventHandler
    {
    
      protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
      {
        ContentService.Published += ContentService_Published;
      }
    
      private void ContentService_Published(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<IContent> e)
      {
          foreach (var newContent in e.PublishedEntities.Where(x => x.IsPropertyDirty("bodyText"))
          {
             var originalContent = Umbraco.Web.UmbracoContext.Current.ContentCache.GetById(newContent.Id);
             // now compare   newContent.GetValue("bodyText") vs originalContent.GetPropertyValue("bodyText")
          }
      }
    }
    

    Cheers

  • andrew shearer 510 posts 659 karma points
    Nov 07, 2016 @ 21:18
    andrew shearer
    1

    great thank you. btw I think you meant the ContentService.Publishing rather than ContentService.Published event.

  • Jonathan Richards 288 posts 1742 karma points MVP
    Nov 08, 2016 @ 10:01
    Jonathan Richards
    0

    You are correct, you did specify publishing event. I was lazy and copied my code from a Published event, strangely I do also have a publishing event I could have used, but for some reason ignored it, because it was bigger.

    Oh well, I'm sure you can amend accordingly.

Please Sign in or register to post replies

Write your reply to:

Draft