Copied to clipboard

Flag this post as spam?

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


  • RunnicFusion 62 posts 145 karma points
    Apr 26, 2016 @ 08:53
    RunnicFusion
    0

    Umbraco 7 UmbracoEvents memberservice not working as expected

    In my Umbraco installation I need to send a email when a new user is created. For that i'm using the Umbraco Events.

    I have the following code:

    protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
     {
          //Events
          MemberService.Saved += MemberService_Saved;
          MemberService.Created += MemberService_Created; 
    }
    

    Saved:

    private void MemberService_Saved(IMemberService sender, SaveEventArgs<IMember> e)
            {
                foreach(var entity in e.SavedEntities)
                {
                    if(HttpContext.Current.Items["member-" + entity.Id] != null)
                    {
                        TaskLogic.NewUser(entity.Id); // send new user email
                        HttpContext.Current.Items["member-" + entity.Id] = null;
                    }
                }
            }
    

    Created:

    private void MemberService_Created(IMemberService sender, Umbraco.Core.Events.NewEventArgs<Umbraco.Core.Models.IMember> e)
            {
                if(e.Entity.Id != 0)
                {
                    HttpContext.Current.Items["member-" + e.Entity.Id] = true;
                }
            }
    

    The TaskLogic class (moved this outside the Umbraco evens for testing, when calling this from a controller it works):

    public static void NewUser(int userId)
            {
                if (userId != 0)
                {
                    // set key for new user so the user can create it's own password afterwards 
                    var memberService = ApplicationContext.Current.Services.MemberService;
                    var member = memberService.GetById(userId);
                    if (member != null)
                    {
                        var randomGuid = Guid.NewGuid().ToString();
                        member.SetValue("passwordKey", randomGuid);
                        memberService.Save(member, false);
                    }
                }
            }
    

    When saving the member in the backoffice the right events are triggered.

    What is the correct way because my events got fired the way i was expecting but the setvalue didn't fire.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Apr 26, 2016 @ 09:04
    Jeroen Breuer
    1

    Hello,

    You set the value in the created event, but you check the value in the saved event. That's wrong because it's already saved. That needs to be in the saving event.

    Also in the created event e.Entity.Id is probably always 0.

    Jeroen

  • RunnicFusion 62 posts 145 karma points
    Apr 26, 2016 @ 09:15
    RunnicFusion
    0

    Hi Jeroen, Thanks for your reply.

    I changed the code a little bit in the way you suggested:

    Saving event (removed the saved event):

    private void MemberService_Saving(IMemberService sender, SaveEventArgs<IMember> e)
        {
            foreach(var entity in e.SavedEntities)
            {
                if(HttpContext.Current.Items["member-" + entity.Id] != null)
                {
                    var randomGuid = Guid.NewGuid().ToString();
                    entity.SetValue("passwordKey", randomGuid);
                    // send mail          
    
                    HttpContext.Current.Items["member-" + entity.Id] = null;
                }
            }
        }
    

    The code is fired but when in look inside Umbraco the property is not filled in.

  • RunnicFusion 62 posts 145 karma points
    Apr 26, 2016 @ 10:30
    RunnicFusion
    0

    When debugging the code the process seems right to me. But the data is not present in the member section afterwards.

    What is the correct way of doing this?

Please Sign in or register to post replies

Write your reply to:

Draft