Copied to clipboard

Flag this post as spam?

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


  • Jules 194 posts 426 karma points
    Dec 10, 2015 @ 16:26
    Jules
    0

    Get previous value of a property from within Published event

    Hi

    Is there a way to access the previous value of a property from within the Published event?

    Jules

  • Ian 178 posts 750 karma points
    Dec 10, 2015 @ 20:58
    Ian
    0

    Hi I think by the time the published event has fired all properties have been updated and persisted to the database. I also think the content cache has been updated so using methods like GetPropertyValue won't work. I think you would need to use ContentService.Saving instead depending on what you are actually trying to do. This event gets called when the user selects to save the item without publishing and when someone publishes an already saved item. Note if it were called on a new entity a condition would need to be included to cater for the possibility that as yet there is no stored data. Pseudo code without checks might look like this:

    public class MySiteEvents : ApplicationEventHandler
    {
        public MySiteEvents()
        {
            ContentService.Saving += HandleOldProperty;
        }
    
        private void HandleOldProperty(IContentService sender, SaveEventArgs<IContent> args)
        {
            var post = args.SavedEntities.FirstOrDefault();
            UmbracoHelper helper = new UmbracoHelper(UmbracoContext.Current);
            var content = helper.TypedContent(post.Id);
            // do checks & get property
        }
    
    
    }
    

    As an alternative to using TypedContent you could do var content = sender.GetById(post.Id); if you don't mind querying the database instead of cache. You might then use the post.Properties to access the property you need

Please Sign in or register to post replies

Write your reply to:

Draft