Copied to clipboard

Flag this post as spam?

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


  • Dan 1285 posts 3917 karma points c-trib
    Mar 29, 2013 @ 17:06
    Dan
    0

    Create member with properties in Umbraco 6.x

    Hi,

    I've developed membership systems on Umbraco 4.x websites before, using code like this:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    using umbraco.cms.businesslogic.datatype;
    using umbraco.cms.businesslogic.web;
    using umbraco.cms.businesslogic.member;
    using umbraco.cms.businesslogic.property;
    
    namespace DocumentMembership
    {
        public partial class Register : System.Web.UI.UserControl
        {
            protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
            {
                // add member to role
                Roles.AddUserToRole(this.CreateUserWizard1.UserName, "WebsiteUser");
    
                // set member property
                Member member = Member.GetMemberFromLoginName(this.CreateUserWizard1.UserName);
                if (member != null)
                {
                    Property FirstNameProperty = member.getProperty("firstName");
                    if (FirstNameProperty != null)
                    {
                        TextBox FirstNameTextBox = (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("FirstName");
                        FirstNameProperty.Value = FirstNameTextBox.Text;
                    }
                    Property SurnameProperty = member.getProperty("surname");
                    if (SurnameProperty != null)
                    {
                        TextBox SurnameTextBox = (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Surname");
                        SurnameProperty.Value = SurnameTextBox.Text;
                    }
                    Property EmailProperty = member.getProperty("email");
                    if (EmailProperty != null)
                    {
                        TextBox EmailTextBox = (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Email");
                        EmailProperty.Value = EmailTextBox.Text;
                    }
                    // Log user in automatically
                    FormsAuthentication.SetAuthCookie(this.CreateUserWizard1.UserName, true);
                    Response.Redirect("/registration-successful/");
                }
            }
    
        }
    }

    Now I'm trying to do the same (essentially, create a member via a registration form, assign the member to a group and populate a couple of properties on the member type, such as 'firstName' and 'lastName') but on Umbraco 6.0.2.

    In Umbraco 6(.0.2) it seems the umbraco membership methods like 'GetMemberFromLoginName' are obsolete.  So instead I'm using the standard .NET membership provider, so I've created the member like this:

    MembershipUser member = System.Web.Security.Membership.GetUser(this.CreateUserWizard1.UserName);

    However, I'm not sure now how to assign the property values (firstName etc) to this member.  I've Googled and I the examples I've found seem to assume more knowledge than I have.  I've also read the umbraco WIKI article but I'm never quite sure if that stuff is up to date and it actually loses me around the 'creating profiles' part (is a profile just the name for a collection of Umbraco member properties, so I need to create a profile on my Umbraco member and then I can set the properties on this as they are in my Umbraco member type?  Then I also need to add all of the properties from the member type to the web.config file?)

    I know this stuff is probably second nature for a .NET dev, which is why it's largely assumed knowledge, but I'd really appreciate some practical pointers here to help me understand it a little more and help to populate my member with the full set of properties.

    Thanks for any pointers folks!

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Mar 29, 2013 @ 17:28
    Tom Fulton
    100

    Hi Dan,

    The way we usually do this is using a ProfileBase class.  See this excellent explanation by Aaron (yes, it's from 2010 but still valid :) ):  http://www.aaron-powell.com/umbraco-members-profiles

    With this setup, we can do things like this after creating the member:

     

    // Save member properties
    var profile = MemberProfile.GetUserProfile(member.UserName);
    profile.FirstName = firstName;
    profile.LastName = lastName;
    profile.ScreenName = username;
    profile.Save();
    Hope this helps,
    Tom 

     

  • Dan 1285 posts 3917 karma points c-trib
    Mar 29, 2013 @ 18:02
    Dan
    0

    Thanks Tom.  Aaron's article seems to be an extended version of the Wiki article, so it gives more background on it, which is useful.  It does seem a bit odd having to delcare member properties in the web.config file, but at least I know that's the right way to do it and having tested just now, it works!

    Thanks

  • Charles Afford 1163 posts 1709 karma points
    Mar 30, 2013 @ 09:46
    Charles Afford
    0

    Hi you dont need to define in the web config and your code. Please see my blog post below. Might be a bit rough. Still finishing it, but should be correct. Charlie :)

    In version 4 of Umbraco the membership model changed from Umbraco's to ASP.net's implementation. Something I have noticed being asked a lot on the Umbraco boards was how to access custom properties against members in code. A lot of the advice pointed towards one of two implementations.  A web config implementation, which meant you have your properties defined in the web config as well as you code or a strongly typed implementation which meant no properties in the web config, but as you will see throws a error in some cases.

     

    For this example we must assume that I have a member in Umbraco with:

     

    User name[email protected]

    Custom property name: FirstName

    Custom property alias: firstname

    Type: Textstring

     

    WEB CONFIG IMPLEMENTATION : This means having properties defined in your web config and your code

     

    <!--WEB CONFIG -->

     <profile defaultProvider="UmbracoMemberProfileProvider" enabled="true">

          <providers>

            <clear/>

            <add name="UmbracoMemberProfileProvider" type="umbraco.providers.members.UmbracoProfileProvider, umbraco.providers"/>

          </providers>

          <properties>

         <add name="surname" allowAnonymous ="false" provider="UmbracoMemberProfileProvider" type="System.String" />

            <clear/>

          </properties>

     <!-- END WEB CONFIG -->

     

    Next we simply get or set the property's from or to our profile base within a model or class.

     

    namespace somenamespace

    {

    public someclass : ProfileBase

    {

    public const FIRSTNAME = “firstname”;

     

    //get the current user by their user name (Umbraco member) 

     

    public static someclass METHODNAME()

    {

    return(someclass)Create(Membership.GetUser().UserName);

    }

     

    //pass the current user's user name as a parameter and setup a profile base

     

    public static someclass GetProfile(string username)

    {

    (someclass)Create(userName);

    }

     

    public string Somemethod(some parameters)

    //Assign the value to the profile base

    base[“FIRSTNAME”] = property;

    //Or get the value from the profile base

    property = base["FIRSTNAME"];

    }

    }

    }

     

    //END OF WEB CONFIG IMPLEMENTATION

     

    STRONGLY TYPED IMPLEMENTATION : This means not having properties defined in your web config 

    Another implementation attempted is strongly typed access to the member properties. 

     

    //No properties defined in the web config 

     

    <!--WEB CONFIG -->

     

    <profile defaultProvider="UmbracoMemberProfileProvider" enabled="true" inherits="BSD.PCSG.Configuration.MemberProfile">

          <providers>

            <clear/>

            <add name="UmbracoMemberProfileProvider" type="umbraco.providers.members.UmbracoProfileProvider, umbraco.providers"/>

          </providers>

          <properties>

          </properties>

    </profile>

     

     <!-- END WEB CONFIG -->

    namespace somenamespace

    {

    public someclass : ProfileBase

    {

    public const FIRSTNAME = “firstname”;

     

    //get the current users user name 

    public static someclass METHODNAME()

    {

    return(someclass)Create(Membership.GetUser().UserName);

    }

     

    //pass the current users user name as a parameter and setup a profile base

    public static someclass GetProfile(string username)

    {

    (someclass)Create(usernName);

    }

     

    public string firstname

    {

    get

    {

    base[“FIRSTNAME”] = value

    }

    set

    {

    value = base[“FIRSTNAME”]

    }

    }

    }

    }

     

    Now after much frustration and a lot of help, the conclusion was that Umbraco was doing some sort of case-insensitive match on the property alias.  Thus if you have a property and alias of the same name it thinks there are two values because they have both been changed to lower case. So what you have to do is insure that the alias and name of your property(s) are not identical when they are both made lower case.

     

    In our example we will changed the alias like so:

     

    User name[email protected] 

    Custom property name: FirstName

    Custom property alias: _firstname

    Type: Textstring

     

    namespace somenamespace

    public someclass : ProfileBase

    {

    public const FIRSTNAME = “_firstname”;

     

    //get the current users user name 

    public static someclass METHODNAME()

    {

    return(someclass)Create(Membership.GetUser().UserName);

    }

     

    //pass the current users user name as a parameter and setup a profile base

    public static someclass GetProfile(string username)

    {

    (someclass)Create(usernName);

    }

    public string _firstname

    {

    get

    {

    base[“FIRSTNAME”] = value

    }

    set

    {

    value = base[“FIRSTNAME”]

    }

    }

    }

    }

     

    Now you should be have strongly typed access to your custom member properties and no properties needing to be defined in the web config.  

    Thanks for reading, please comment, suggest new topics and correct me if you think there is a better way of doing something

    Charlie :).

Please Sign in or register to post replies

Write your reply to:

Draft