Copied to clipboard

Flag this post as spam?

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


  • Emanuel 6 posts 76 karma points
    Feb 25, 2016 @ 14:30
    Emanuel
    0

    Changing member property value on member created event not working.

    I am creating a new member and I want to set a value for a property of member on member Created event. The Saved and Saving event fires 3 times (maby last edited and other events fired those events) and on the last Saved event the value is null again.

    private void MemberService_Created(Umbraco.Core.Services.IMemberService sender, Umbraco.Core.Events.NewEventArgs<Umbraco.Core.Models.IMember> e)
        {
            var vt = Guid.NewGuid();
    
            e.Entity.SetValue("validationToken", vt.ToString());            
    
            //applicationContext.Services.MemberService.Save(e.Entity); 
    
                   }
    

    I have tried using save method on member but still not working. Any help pls?

    I am using v7.4.1

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Feb 25, 2016 @ 21:45
    Marc Goodson
    2

    Hi Emanuel

    Have a read of this issue:

    http://issues.umbraco.org/issue/U4-6366

    basically the Created event isn't really what you would expect it to be, eg the 'member is all done and successfully created and now I can do stuff with it' that is the 'Saved' Event (Created is obsolete) - however since you want to change the value of a custom property, then you may as well do this on the Saving event, to avoid saving twice.

    But as you've pointed out there is another issue:

    http://issues.umbraco.org/issue/U4-7085

    Where the Saving and Saved events are being called three times... and the last time it doesn't appear to be aware of anything else that happened on the other two times...

    I tried this on the MemberService.Saving event:

     void MemberService_Saving(IMemberService sender, Umbraco.Core.Events.SaveEventArgs<IMember> e)
            {
                var vt = Guid.NewGuid();
                var savedEntity = e.SavedEntities.FirstOrDefault();
                if (savedEntity != null)
                {
                   var notAlreadySet = String.IsNullOrEmpty(savedEntity.GetValue<string>("validationToken"));
                   if (notAlreadySet)
                   {
                       savedEntity.SetValue("validationToken", vt.ToString());
                   }
                }
            }
    

    The first time it calls the event, notAlreadySet is true, so the Guid is set on the property

    The second time it calls the event, it's all ok, notAlreadySet is false; and if you inspect the member, the guid is still there from the first Saving Event call.

    The Third time however, notAlreadySet is back to true, and the Guid field is blank, the code above sets it again, but obviously with a different Guid, as to the first save.

    I'm not sure if the consistency of this behaviour enables you to workaround ? but vote up the issues all the same.

    I guess the other way around this would be to write something outside of the events that was scheduled to run regularly, and used the CreateDate, to discover new Members...

    regards

    Marc

  • Emanuel 6 posts 76 karma points
    Feb 26, 2016 @ 05:56
    Emanuel
    0

    Thanks Marc,

    Unfortunately I have to send an email with the validation token so I can not afford to change it as the email will be invalid.

    Yes, I think a workaround will be to use a web job or a worker role.

    Anyway thanks for the very detailed answer and your time.

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Nov 30, 2020 @ 17:25
    Alex Skrypnyk
    0

    Hi Marc,

    I'm experiencing the same problem in v8.

    Are any ways of dealing with custom properties setup on member create an event?

    I'm trying to use Member.Saving event, but it became null when .Saving event rises third time.

    Thanks, Alex

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Nov 30, 2020 @ 20:21
    Marc Goodson
    1

    Hi Alex

    I'm not sure of the context that you are trying to achieve, I did find this thread though on the issue tracker:

    https://github.com/umbraco/Umbraco-CMS/issues/3838

    maybe that would shine some light on why this occurs, and possible workarounds for what you are trying to achieve.

    regards

    marc

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Dec 04, 2020 @ 10:12
    Alex Skrypnyk
    0

    Hi Marc,

    Thank you, it helped. #h5YR

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft