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
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
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:
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
is working on a reply...