Copied to clipboard

Flag this post as spam?

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


  • Jens Due 2 posts 82 karma points
    Oct 04, 2022 @ 08:28
    Jens Due
    0

    How to use MemberSavingNotification to get values from original member?

    Hi

    I would like to get notified when the name of a member has changed. I am running on version 10.0

    How do I get the original member in MemberSavingNotification (or MemberSavedNotification) ?

     public void Handle(MemberSavingNotification notification)
     {
         var entities = notification.SavedEntities;
         // do stuff
    

    The member obejcts in notification.SavedEntities is the updated object, and do not have the original values. I need access to the original object, so I can see what properties has changes.

    Or is there another way to get the original member object?

    Thanks,

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Oct 04, 2022 @ 08:34
    Sebastiaan Janssen
    100

    You should be able to cast the entity to see which properties are dirty: https://our.umbraco.com/documentation/reference/events/determining-new-entity

    Something like:

     public void Handle(MemberSavingNotification notification)
     {
         var entities = notification.SavedEntities;
         // do stuff
         foreach(var entity in entities)
         {
             var dirty = (IRememberBeingDirty)entity;
             var nameChanged = dirty.WasPropertyDirty("name");
         }
    

    I'm not entirely sure if you can use "name" but have a look in the debugger and see what it looks like.

  • Jens Due 2 posts 82 karma points
    Oct 04, 2022 @ 09:29
    Jens Due
    0

    Perfekt Sebastiaan! That solved the problem.

    To other readers of this thread; there are two small issues:

    • The code has to be located in the 'after' notification to mark the properties are dirty. So use the MemberSavedNotification.
    • Find the name of the property in the debugger.

    The code now looks like this:

    public void Handle(MemberSavedNotification notification)
    {
        var entities = notification.SavedEntities;
        foreach (var entity in entities)
        {
            var dirty = (IRememberBeingDirty)entity;
            var nameChanged = dirty.WasPropertyDirty("Name");
            if (nameChanged)
            {
                _logger.LogInformation("Name has changed");
            }
        }
    

    Thanks

Please Sign in or register to post replies

Write your reply to:

Draft