Copied to clipboard

Flag this post as spam?

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


  • Daniel Larsen 116 posts 381 karma points
    Sep 19, 2014 @ 10:30
    Daniel Larsen
    0

    Set member property u7

    Hi

    So I want to give a member property a new value and I had it working in u4, but it is not working in u7. What changed?

    Original thread

    @{
        var m = new Member(Convert.ToInt32(System.Web.Security.Membership.GetUser().ProviderUserKey));
    
        var local = m.getProperty("PhoneLocal").Value.ToString();
        var nLocal = Request.QueryString["local"];
    
        if(!String.IsNullOrEmpty(@nLocal)) 
        {
            if(@nLocal != @local) 
            {
                m.getProperty("PhoneLocal").Value = @nLocal;
            }
        }
    }
    

    Thank you for the help!

    /Daniel

  • Dan Lister 416 posts 1974 karma points c-trib
    Sep 19, 2014 @ 10:37
    Dan Lister
    1

    Hi Daniel,

    You'll probably want to use the new Member Service to update a member property:

    // Find the current member
    var username = System.Web.Security.Membership.GetUser().UserName;
    var member = Umbraco.Core.ApplicationContext.Services.MemberService.GetByUsername(username);
    
    // Update the member
    member.SetValue("alias", "value");
    
    // Save the member
    Umbraco.Core.ApplicationContext.Services.MemberService.Save(member);
    

    Thanks, Dan.

  • Daniel Larsen 116 posts 381 karma points
    Sep 19, 2014 @ 11:00
    Daniel Larsen
    0

    Thank you! It probably works, but i get this error:

    An object reference is required for the non-static field, method, or property 'Umbraco.Core.ApplicationContext.Services.get

    What reference am I missing?

    Is there an easier way of retrieving member properties, that the one I use above? Do you have a link for some documentation on the new member service? :-)

    /Daniel

  • Dan Lister 416 posts 1974 karma points c-trib
    Sep 19, 2014 @ 11:51
    Dan Lister
    1

    Hi Daniel,

    You can find more information on the Member Service here.

    Where are trying to update the member? On a template or within a controller for example? Secondly, does the property exist on the member type you are trying to update and is the alias PhoneLocal correct?

    Thanks, Dan.

  • Daniel Larsen 116 posts 381 karma points
    Sep 19, 2014 @ 12:23
    Daniel Larsen
    0

    I am making a macroscript with this code. The property does exist on that membertype.

    I added this, but stiil get the error:

    @using Umbraco.Core;

    @using Umbraco.Core.Models;

    @using Umbraco.Core.Services;

    Is it right or am I doing it wrong?

    /Daniel

  • Dan Lister 416 posts 1974 karma points c-trib
    Sep 19, 2014 @ 15:28
    Dan Lister
    100

    Hmmm strange. Come to think of it, you shouldn't need the using statements as mentioned above. Also, try changing your service namespace references to something like the following. I've also added an extra check to see if the current member can be found:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        // Find the current member
        var membershipUser = Membership.GetUser();
        if (membershipUser != null)
        {
            var membershipUsername = membershipUser.UserName;
            var member = ApplicationContext.Services.MemberService.GetByUsername(membershipUsername);
    
            // Update the member
            member.SetValue("PhoneLocal", "0123456789");
    
            // Save the member
            ApplicationContext.Services.MemberService.Save(member);
        }
    }
    

    Thanks, Dan.

  • Daniel Larsen 116 posts 381 karma points
    Sep 19, 2014 @ 15:39
    Daniel Larsen
    0

    Now i get this error message: Context Must Implement System.Web.WebPages.WebPage

    /Daniel

  • Dan Lister 416 posts 1974 karma points c-trib
    Sep 19, 2014 @ 15:45
    Dan Lister
    1

    Have you created the script as a Macro Script (~/MacroScripts) or a Partial View Macro (~/Views/MacroPartials)? If its the former, then you will want to create it is a Partial View Macro instead and use that with your macro.

    Thanks, Dan.

  • Daniel Larsen 116 posts 381 karma points
    Sep 22, 2014 @ 09:43
    Daniel Larsen
    1

    Thank you, it works! I made a partial view and that did the trick :-)

    /Daniel

Please Sign in or register to post replies

Write your reply to:

Draft