Copied to clipboard

Flag this post as spam?

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


  • Steven McLintock 12 posts 56 karma points
    Jul 15, 2014 @ 14:01
    Steven McLintock
    1

    Update a member property in Umbraco 7

    Hi again,

    I'm trying to now update a custom property for a member. I have been looking through the Umbraco documentation, but it appears to be out of date as none of what I try appears to work?

    // Get the details of the user currently logged in
    var profileModel = Members.GetCurrentMemberProfileModel();
    
    var profile = ProfileBase.Create(profileModel.Email);
    
    profile.SetPropertyValue("city", model.City);
    // profile["city"] = model.City;
    
    profile.Save();
    

    I get the error message "The property "city" is not recognised" on any of the above attempts.

    Thanks for any help!

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Jul 15, 2014 @ 16:19
    Dennis Aaen
    0

    Hi Steven,

    As I said yesterday I am not a backend developer, but I will try to help, as much as I can.

    Have you seen this documentation about MemberShipHelpers:

    http://our.umbraco.org/Documentation/Reference/Querying/MemberShipHelper/index

    There is a method, called .UpdateMemberProfile(); in the bottom of the, maybe you can use this method to update your profile.

    Hope this helps,

    /Dennis

  • Dan 1285 posts 3917 karma points c-trib
    Jul 15, 2014 @ 17:04
    Dan
    0

    Hi Steven,

    For Umbraco 7 it would be best to use the new Member Service API - the documentation is up to date: http://our.umbraco.org/documentation/Reference/Management-v6/Services/MemberService.

    Here's how you'd update a custom member property - assuming you have a member and the member is logged in:

    // Get the member
    var member = Member.GetCurrentMember();
    // Check if there is a current member if (member != null) {
    // Update member properties member.getProperty("city").Value = model.City;

    // Save the updated member member.Save();
    }

    Hope this helps...

  • Steven McLintock 12 posts 56 karma points
    Jul 15, 2014 @ 18:36
    Steven McLintock
    0

    Hi Dan,

    If I try "var member = Member.GetCurrentMember();" I get the error message "The name Member does not exist in the current context"? I have also tried "var member = Umbraco.Core.Services.Member.GetCurrentMember();" to no avail.

    Thanks, Steven.

  • Dan 1285 posts 3917 karma points c-trib
    Jul 15, 2014 @ 18:41
    Dan
    0

    Where is this happening (razor view, controller etc)?  What assemblies are you referencing?  Are you creating a member and updating the properties in one go, or do you already have the member created and logged in?

  • Steven McLintock 12 posts 56 karma points
    Jul 15, 2014 @ 18:54
    Steven McLintock
    0

    Hi Dan,

    Thanks for replying, appreciate it. This is in a controller and the user is already logged in at this point.

    The assembly references I am using are:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using UmbracoCMS.Models;
    

    Thanks again, Steven.

  • Dan 1285 posts 3917 karma points c-trib
    Jul 15, 2014 @ 19:00
    Dan
    0

    Try adding these references:

    using umbraco.cms.businesslogic.member;
    using Umbraco.Core;
    using Umbraco.Core.Services;
  • Steven McLintock 12 posts 56 karma points
    Jul 16, 2014 @ 11:55
    Steven McLintock
    5

    Hi again,

    I did some research and it turns out Umbraco 7 has overhauled the Member Service API, which is why very little of the documentation I can find online is actually working. In case anyone else is looking at how to do this in future, I've copied the working code below:

            // Check if the user is logged in
            if (!User.Identity.IsAuthenticated)
            {
                // Send the user to the login page
                Redirect("/login");
            }
    
            // The model is not valid yet
            if (!ModelState.IsValid)
            {
                // Display the current Umbraco page (do not redirect)
                return CurrentUmbracoPage();
            }
    
            // Create new instance of "MemberService"
            var memberService = Services.MemberService;
    
            // Get the details of the user currently logged in
            var profileModel = Members.GetCurrentMemberProfileModel();
    
            // Get the custom properties for the member
            var member = memberService.GetByEmail(profileModel.Email);
    
            // Check if the email address is in the database
            if (member == null)
            {
                ModelState.AddModelError("", "Sorry, we could not find the user to update");
                return CurrentUmbracoPage();
            }
    
            // Update the member properties
            member.Properties["city"].Value = model.City;
    
            // Save in Umbraco
            memberService.Save(member);
    
  • Streety 358 posts 568 karma points
    Sep 21, 2016 @ 14:49
    Streety
    0

    This didn't work for me.

    Members.GetCurrentMemberProfileModel();
    

    ...yielded a null value, it seems MemberService is the only way to go as the Membership helper is broke.

    However this did work for me.

    // Create new instance of "MemberService"
    var memberService = Services.MemberService;
    
    // Expose the custom properties for the member
    var member = memberService.GetByEmail(model.EmailAddress);
    
    // Update the member properties
    member.Properties["company"].Value = "Set Property";
    
    // Save the object
    memberService.Save(member);
    

    The membership helper is only used to get the current email address anyhow which was in my login page model already.

    Note the square brackets getting your property references.

    As "umbraco.cms.businesslogic.member" is now deprecated its the only way to go without rolling your own.

Please Sign in or register to post replies

Write your reply to:

Draft