Copied to clipboard

Flag this post as spam?

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


  • Jason Evans 50 posts 71 karma points
    Dec 18, 2012 @ 16:57
    Jason Evans
    0

    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?

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jan 02, 2013 @ 16:28
    Sebastiaan Janssen
    0

    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). 

    d.RollBack(new Guid(d.Version), new User(0));

    And you can get the already saved values by just getting the document again:

    var savedDoc = new Document(d.Id);

    I've never tried this but it might just work.

  • Jason Evans 50 posts 71 karma points
    Jan 02, 2013 @ 16:32
    Jason Evans
    0

    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 :)

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jan 02, 2013 @ 16:33
    Sebastiaan Janssen
    0

    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. :)


  • Jason Evans 50 posts 71 karma points
    Jan 02, 2013 @ 17:24
    Jason Evans
    0

    I gave the following a shot

     

    private Guid prevVersion;
    private void OnBeforeDocumentSave(Document sender, SaveEventArgs e)
    {
        this.prevVersion = sender.Version;
    }
    private void CheckForNationalNodeIdentifier(object sender, SaveEventArgs e)
    {
        var doc = sender as Document;
        if(!this.documentValidator.DocumentValid(doc))
        {
            var currentUser = // Get current user.
            doc.RollBack(this.prevVersion, currentUser);
        }
    }

    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?

     

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jan 02, 2013 @ 17:30
    Sebastiaan Janssen
    0

    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;
  • Jason Evans 50 posts 71 karma points
    Jan 02, 2013 @ 18:28
    Jason Evans
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft