Search code examples
c#.netumbracoumbraco7

Umbraco 7, How to change a custom property in MemberService.Saved


I am struggling with changing a simple true / false flag in member properties once the Is Approved flag is set to true for the first time. I can change the property but the value is not saved / committed. I have tried both MemberService.Saved and MemberService.Saving. I am quite new to Umbraco so may have missed something obvious.

protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
    MemberService.Saved += MemberService_Saved;
}

void MemberService_Saved(IMemberService sender, Umbraco.Core.Events.SaveEventArgs<IMember> e)
{ 
    foreach (var member in e.SavedEntities)
    {
        if (!member.IsNewEntity())
        {
            var dirtyProperties = member.Properties.Where(x => x.WasDirty()).Select(p => p.Alias);
            if (dirtyProperties.Contains("umbracoMemberApproved"))
            {
                if (member.IsApproved && !member.GetValue<bool>("approvalEmailSent"))
                {
                    //Send Email to Customer
                    //new SmtpClient().Send(mail);

                    var prop = member.Properties["approvalEmailSent"];
                    prop.Value = true;

                    var propValue = member.GetValue<bool>("approvalEmailSent");
                    //Have verified propValue is now true

                    sender.Save(member);
                } 
            }
        }
    }
}

Strangely I can find another member, make the property change and save it fine, just not the member sent through in e.SavedEntities.

TIA


Solution

  • This appears to me to be some sort of bug, after much debugging I have found the following:

    • Even when setting the raise events flag to false on the save, it still comes back through the method again.
    • On the second pass through you must set the value of the property you are changing again, even though it should already be saved / committed. I did not need to save again. I believe this is a bug
    • The original changed by the user property's was dirty flag is no longer set the second time through. So the original check for this was stopping the property I want to change being set again.
    • I had to have the was dirty check so that on the second pass through I did not send the email again.
    • As a note even changing something unrelated in a member using the umbraco front end will cause my method to run twice.
    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        MemberService.Saved += MemberService_Saved;
    }
    
    void MemberService_Saved(IMemberService sender, Umbraco.Core.Events.SaveEventArgs<IMember> e)
    {
        foreach (var member in e.SavedEntities)
        {
            if (!member.IsNewEntity())
            {
                if (member.IsApproved && !member.GetValue<bool>("approvalEmailSent"))
                {
                    member.SetValue("approvalEmailSent", true);
    
                    var dirtyProperties = member.Properties.Where(x => x.WasDirty()).Select(p => p.Alias);
                    if (dirtyProperties.Contains("umbracoMemberApproved"))
                    {
                        //Email Customer
                        //new SmtpClient().Send(mail);
    
                        sender.Save(member, false);
                    }
                }
            }
        }
    }