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.
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.
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...
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.
I have tried using save method on member but still not working. Any help pls?
I am using v7.4.1
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:
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
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.
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
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
Hi Marc,
Thank you, it helped. #h5YR
Alex
is working on a reply...