Copied to clipboard

Flag this post as spam?

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


  • Martin 8 posts 39 karma points
    Dec 02, 2014 @ 13:03
    Martin
    0

    MemberProperties returning only properties with memberCanEdit = true

    Hi all. I have a question regarding the ProfileModel in Umbraco 7.1.8

    I have created a number of custom properties on the default membertype, some are set as editable by the user, some are not. I want to manipulate all of these properties in backend code, but it turns out that the profileModel.MemberProperties only contains properties that have memberCanEdit = true.

    How can I access the other properties in backend?

    My way of getting the properties:

    var membershipHelper = new Umbraco.Web.Security.MembershipHelper(Umbraco.Web.UmbracoContext.Current);
    var pm = membershipHelper.GetCurrentMemberProfileModel();
    List<Umbraco.Web.Models.UmbracoProperty> properties = pm.MemberProperties;
  • Dan 1288 posts 3921 karma points c-trib
    Dec 02, 2014 @ 14:22
    Dan
    100

    Hi Martin,

    I think you'll need to use the member service for that.  This code is just from memory so it'll probably need tweaking, but something like this should do the trick:

    // Instantiate member service and membership helper
    var memberService = ApplicationContext.Current.Services.MemberService; var membershipHelper = new MembershipHelper(UmbracoContext);

    // Get profile model of current member var profileModel = membershipHelper.GetCurrentMemberProfileModel();

    // Get username of current member var username = profileModel.UserName;

    // Use member service to get current member object by username (could do it from email and/or id too I think...) var currentMember = memberService.GetByUsername(username);
    // Update custom member properties currentMember.SetValue("yourCustomProperty", "Some value");

    // Save updated member memberService.Save(currentMember);
    Hope this helps.
  • Martin 8 posts 39 karma points
    Dec 02, 2014 @ 16:49
    Martin
    0

    Hi Dan. Almost there I think, but as I'm coding a usercontrol, I don't think I have the ApplicationContext available?

    So how do I instantiate a MemberService object? Will the following work? (Seems a bit far fetched, and not at my laptop so can't try it out right now)

    var ms = new MemberService( new RepositoryFactory(), new MemberGroupService( new RepositoryFactory()) );

    Thanks

    Martin

  • Dan 1288 posts 3921 karma points c-trib
    Dec 02, 2014 @ 17:20
    Dan
    0

    Hi Martin,

    You should be able to instantiate the member service outside of the `ApplicationContext` like this:

    using Umbraco.Core;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    
    var memberService = Services.MemberService;

    There's stuff in the documentation about this actually - http://our.umbraco.org/documentation/Reference/Management-v6/Services/MemberService.

  • Martin 8 posts 39 karma points
    Dec 03, 2014 @ 19:14
    Martin
    0

    Hmm, my approach above did indeed work, so I guess I can stick with that. I can't get your last approach to work, as I get the following error when I try to assign to the variable as you do above. (I am writing a .net usercontrol, and have included the umbraco libraries).

    'Umbraco.Core.Services.MemberService' is a 'type', which is not valid in the given context.

     

  • jivan thapa 194 posts 681 karma points
    Dec 03, 2014 @ 20:18
    jivan thapa
    0

    Hey, Martin.

    Have you try using MemberType, it has possibilities to iterate the properties?

    var mHelper = ApplicationContext.Current.Services.MemberService;
        // "Member" is alias of default MemberType
        var memberType = mHelper.GetMembersByMemberType("Member").FirstOrDefault();
        if (memberType != null)
        {
            foreach (var mProperty in memberType.Properties)
            {
                @mProperty.Alias
            }
        }
    
  • Martin 8 posts 39 karma points
    Dec 05, 2014 @ 19:42
    Martin
    1

    Hi all.
    I got it to work doing the following. Dan let me in the right direction. I still can't figure out if I get the MemberService instance in the right way, but it works.

    var memberService = newMemberService(new Umbraco.Core.Persistence.RepositoryFactory(), newMemberGroupService(new Umbraco.Core.Persistence.RepositoryFactory()));
    var memberShipHelper = newMembershipHelper(Umbraco.Web.UmbracoContext.Current);
    var currentMember = memberService.GetByUsername(memberShipHelper.GetCurrentMemberProfileModel().UserName);
    var
    properties = currentMember.Properties;

     

    This way I can use a LINQ query on the PropertyCollection 'properties' to find whichever custom property I need, using it's value or whatever parameter I like.
    Modifying values, you must use the method Dan described above, using

    // Update custom member properties
    currentMember
    .SetValue("yourCustomProperty","Some value");

    // Save updated member
    memberService
    .Save(currentMember);

  • Dan 1288 posts 3921 karma points c-trib
    Dec 06, 2014 @ 11:40
    Dan
    0

    Excellent, glad you got it sorted Martin :)

    As a complete aside, this article from yesterday's Umbraco advent calendar is a really nice reference for real-world membership integration: http://24days.in/umbraco/2014/dealing-with-members/.

Please Sign in or register to post replies

Write your reply to:

Draft