Copied to clipboard

Flag this post as spam?

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


  • Euge 23 posts 130 karma points
    Jun 23, 2016 @ 10:00
    Euge
    0

    Add custom property to Membership member

    Hi

    I need to programmatically add a custom property an umbraco member using the standard membership provider. I'm using Umbraco 7.3.

    So I create the member below (is this correct?) then I need to add a custom property that will store simply a list of integers.

    What approach do I take?

                var userToSave = _umbracoHelper.MembershipHelper.CreateRegistrationModel();
                userToSave.Name = "test;
                userToSave.Email = "[email protected]";
                userToSave.Password = "xxxxxxxxxxxx";
                userToSave.UsernameIsEmail = true;
                Members.RegisterMember(userToSave, out status, true);
    
  • Dennis Aaen 4492 posts 18198 karma points admin hq c-trib
    Jun 23, 2016 @ 10:19
    Dennis Aaen
    1

    Hi Euge,

    Try to see our documentation about the member service, there you have the create method and others.

    https://our.umbraco.org/documentation/reference/management/services/memberservice

    Or in this video on Umbraco TV

    https://umbraco.tv/videos/umbraco-v7/developer/fundamentals/member-api/

    Hope this helps,

    /Dennis

  • Euge 23 posts 130 karma points
    Jun 23, 2016 @ 10:25
    Euge
    0

    Great thanks Dennis

    There doesn't appear to be anything on adding a custom property but I am thinking along the lines of

    var userToSave = _umbracoHelper.MembershipHelper.CreateRegistrationModel();
            userToSave.Name = "test;
            userToSave.Email = "[email protected]";
            userToSave.Password = "xxxxxxxxxxxx";
            userToSave.UsernameIsEmail = true;
           // add custom property here?  
          userToSave.MemberProperties.Add(.....
    
  • Dennis Aaen 4492 posts 18198 karma points admin hq c-trib
    Jun 23, 2016 @ 12:29
    Dennis Aaen
    1

    Hi Euge,

    This video should show you have to set custom property values on a member using the Member API

    http://umbraco.tv/videos/umbraco-v7/developer/fundamentals/member-api/populating-property-values/

    Hope this helps,

    /Dennis

  • Micha Somers 134 posts 597 karma points
    Jun 23, 2016 @ 19:52
    Micha Somers
    2

    Hi Euge,

    When you have a custom member type defined in Umbraco with some extra properties (eg. a property named customMemberInformation), the following code can be used (for example in your own SurfaceController):

    ...

            MembershipCreateStatus status;
            var member = Members.RegisterMember(model, out status, model.LoginOnSuccess);
    
            // Handle custom fields
            var memberService = Services.MemberService;
            var customMember = memberService.GetByEmail(member.Email);
            customMember.SetValue("customMemberInformation", "My custom member information");
            memberService.Save(customMember);
    

    ...

    In this simplified example I store the text "My custom member information" in the custom field and save the data using the MemberService.

    Does this help you move ahead?

  • Jonathon Cove 12 posts 83 karma points
    Jan 13, 2022 @ 10:10
    Jonathon Cove
    0

    Just to note that in the above example, customMemberInformation needs to be created in the back office.

  • Jonathon Cove 12 posts 83 karma points
    Jan 13, 2022 @ 11:43
    Jonathon Cove
    0

    I stumbled on an answer for this here https://our.umbraco.com/forum/using-umbraco-and-getting-started/106096-add-property-to-media-type-programatically

    To create a custom property for a member you have to add it to the member type, by creating a new property type based on an existing data type. This makes sense if you think of how it works in the backoffice.

    See below, get the 'Label (string)' data type, and create a new PropertyType from it (there are a few overloads when creating, but you need to make sure Name and Alias are set, which none of them do). Then you assign it to the member type and save. Saving the member type has the knock of effect of creating the property type.

    var memberType = _memberTypeService.Get("Member");
    
    if (!memberType.PropertyTypeExists(propertyAlias)) {
        var dataType = _dataTypeService.GetDataType("Label (string)");
        var newProp = new PropertyType(dataType); //object needs to be saved    
        newProp.Name = propertyName;
        newProp.Alias = propertyAlias;
    
        bool success = memberType.AddPropertyType(newProp, propertyGroupName);
        _memberTypeService.Save(memberType);
    }
    

    Then you need to re-get the member before setting the new property type.

    See below a full example:

    //get the member and member type
    var memberType = _memberTypeService.Get("Member");
    var currentMember = _memberService.GetByEmail("[email protected]");
    
    //setup some properties
    string propertyGroupName = "Test Group";
    string propertyName = "Test Property";
    string propertyAlias = "testProperty";
    
    //create the property group if it doesn't exist
    if (!memberType.PropertyGroups.Any(pg => pg.Name == propertyGroupName))
    {
        bool success = memberType.AddPropertyGroup(propertyGroupName);
        _memberTypeService.Save(memberType);
    }
    
    //create the property if it doesn't exist
    if (!memberType.PropertyTypeExists(propertyAlias))
    {
        var dataType = _dataTypeService.GetDataType("Label (string)");
        var newProp = new PropertyType(dataType); //object needs to be saved    
        newProp.Name = propertyName;
        newProp.Alias = propertyAlias;
    
        bool success = memberType.AddPropertyType(newProp, propertyGroupName);
        _memberTypeService.Save(memberType);
    }
    
    currentMember = _memberService.GetByEmail(currentMember.Email);
    currentMember.SetValue(propertyAlias, "456");
    
    _memberService.Save(currentMember);
    

    I used dependency injection to get the _memberTypeService and _dataTypeService.

    The above works in 8.15.1

Please Sign in or register to post replies

Write your reply to:

Draft