BeforeSave event - why does the passed Document object contain old values?
I have an event listener in my solution that listens out for "BeforeSave" events for Documents. The idea is that I'd like to validate a property on a doucment and cancel the save based on whether the validation failed (e.g. e.Cancel = true)
My problem is this - the Document instance that is passed to "BeforeSave" handlers contains all the old data and not the data that I have just entered on the screen. This was unexpected to me, as I was hoping to see the new values so that I can do the validation.
Is this how "BeforeSave" works? I cannot do the validation on "AfterSave" since the data will have already been saved to the database by then, which is too late.
Is there another approach I could use to achieve what I want?
I think the only option you have is, in the AfterSave event, do a rollback in the to the previous version if validation fails. I found this, not sure if it works (d is the sender, your document).
d.RollBack(new Guid(d.Version), new User(0));
And you can get the already saved values by just getting the document again:
Neh, as I said I didn't test this, so I'm not sure if and how it works. You might want to use d.GetVersions() then, something like (assuming the previous version is the last on in the list):
var count = d.GetVersions().Count();
this.prevVersion = d.GetVersions().Skip(count - 1).Take(1).Single().Version;
BeforeSave event - why does the passed Document object contain old values?
I have an event listener in my solution that listens out for "BeforeSave" events for Documents. The idea is that I'd like to validate a property on a doucment and cancel the save based on whether the validation failed (e.g. e.Cancel = true)
My problem is this - the Document instance that is passed to "BeforeSave" handlers contains all the old data and not the data that I have just entered on the screen. This was unexpected to me, as I was hoping to see the new values so that I can do the validation.
Is this how "BeforeSave" works? I cannot do the validation on "AfterSave" since the data will have already been saved to the database by then, which is too late.
Is there another approach I could use to achieve what I want?
Sorry, you won't be able to do this with the current API as it's pretty crappy: http://our.umbraco.org/forum/developers/extending-umbraco/2275-beforepublish-and-beforesave-event-handlers
I think the only option you have is, in the AfterSave event, do a rollback in the to the previous version if validation fails. I found this, not sure if it works (d is the sender, your document).
And you can get the already saved values by just getting the document again:
I've never tried this but it might just work.
Many thanks for your reply, very much appreciated.
I will look at using your suggested way of performing a rollback, since that would be a lifesaver for a feature we are looking to implement.
Thanks :)
If you're on 4.11.x then the upgrade to v6 should be relatively painless, we'll release it soon!
In v6 the API is much better and you can do whatever you want before values are actually saved. :)
I gave the following a shot
But this fails to rollback to the previous version because the Guid values of "prevVersion" and "doc.Version" are the same.
Again, I might be going around this wrong way - I may have misunderstood about picking up the old version ID for the document?
Neh, as I said I didn't test this, so I'm not sure if and how it works. You might want to use d.GetVersions() then, something like (assuming the previous version is the last on in the list):
Apologies for missing GetVersions(), didn't spot that method initially.
That works a charm, as I can now rollback to the previous version by doing doc.GetVersions().First().Version.
Thanks for your help.
is working on a reply...