Copied to clipboard

Flag this post as spam?

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


  • Tyler Brown 62 posts 209 karma points
    Jan 25, 2016 @ 13:54
    Tyler Brown
    0

    Event Handler to change property of Member on Created

    Hello fellow Umbracians,

    I'm running Umbraco 7.2.8 and I need to save some data in a member property (label or textstring) when a new member is created. The following code does not work. The property with alias "certificationID" is not being populated. From the code, you can see I've tried changing the property value two different ways. neither way works. Any ideas? I've not dealt with events or members in Umbraco 7 so maybe I'm missing something? I'm not getting any clues from the console log...

    public class CreateCertificationID : IApplicationEventHandler
    {
    
    
        void IApplicationEventHandler.OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
        }
    
        void IApplicationEventHandler.OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
        }
    
        void IApplicationEventHandler.OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
    
            MemberService.Created += MemberService_Created;
    
        }
    
        void MemberService_Created(IMemberService sender, Umbraco.Core.Events.NewEventArgs<Umbraco.Core.Models.IMember> e)
        {
    
            if (e.Entity.ContentType.Alias == "Member")
            {
                e.Entity.SetValue("certificationID", "0123456789");
                e.Entity.Properties["certificationID"].Value = "0123456789";
    
                sender.Save(e.Entity);
            }
        }
    
    }
    
  • Thomas Beckert 193 posts 469 karma points
    Jan 25, 2016 @ 14:01
    Thomas Beckert
    0

    Hi,

    I saved the property this way, maybe this works better:

     e.Entity.SetValue("cetificationID",12345);             
                ApplicationContext.Current.Services.MemberService.Save(e.Entity);
    
  • Tyler Brown 62 posts 209 karma points
    Jan 25, 2016 @ 14:20
    Tyler Brown
    0

    Thomas,

    Thanks for the reply! This does not work for me either. I'm not getting any errors, but my text box is still empty... but the fact that it is working for you may mean I've got something else hindering the save?

  • Thomas Beckert 193 posts 469 karma points
    Jan 25, 2016 @ 15:13
    Thomas Beckert
    0

    Have you set a breakpoint to see if your code really steps into the if-Condition?

  • Tyler Brown 62 posts 209 karma points
    Jan 25, 2016 @ 17:22
    Tyler Brown
    0

    I've checked that. I've added tracepoints noting the property value before and after save and they look correct.

  • Thomas Beckert 193 posts 469 karma points
    Jan 25, 2016 @ 18:16
    Thomas Beckert
    0

    Mh, ok. Give this a try:

    var member = ApplicationContext.Services.MemberService.GetById(e.Entity.Id); 
    member.SetValue("cetificationID",12345); 
    ApplicationContext.Current.Services.MemberService.Save(member);
    
  • Tyler Brown 62 posts 209 karma points
    Jan 26, 2016 @ 14:43
    Tyler Brown
    0

    I've got to be missing something. I tried your code above and I get the same result. I've thrown in some traces to my original and here is what I get:

    enter image description here

    and below is the debug output:

    Property Value line 40: null entity id line 44: 1496 Property Value line 47: "something" Property Value line 49: "something" A first chance exception of type 'System.NullReferenceException' occurred in Umbraco.Core.dll A first chance exception of type 'System.NullReferenceException' occurred in Umbraco.Core.dll A first chance exception of type 'System.InvalidCastException' occurred in mscorlib.dll A first chance exception of type 'System.InvalidCastException' occurred in Umbraco.Core.dll The thread 'Lucene Merge Thread #0' (0x2564) has exited with code 0 (0x0).

    So it is finding the correct member and it is saving the property value. At line 50 my property value is the value that I've saved, but it doesn't show up on the frontend. I'm also not sure what these 'first chance System.NullReferenceExceptions are all about, but I'm not sure how to get more information on those... any ideas?

  • Tyler Brown 62 posts 209 karma points
    Jan 26, 2016 @ 16:55
    Tyler Brown
    0

    Looking into it further, looks like the same issue as described in this post:

    https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/52676-Add-Member-to-MemberGroup-in-MemberServiceCreated-handler

    I think I'm going to have to abandon this and try to solve my problem a different way...

Please Sign in or register to post replies

Write your reply to:

Draft