Copied to clipboard

Flag this post as spam?

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


  • Sean 141 posts 179 karma points
    Mar 15, 2011 @ 12:17
    Sean
    0

    Insert members into a specific group

    Hi There,

    I'm wanting to insert a member into a specific group. I'm getting a null reference error when I try to do this, I'm un sure of the syntax, can someone help me out with this issue please?

    And perhaps point me in the direction of the API reference too?

    Thanks in advance.

    Sean

     

                MemberType mt = MemberType.GetByAlias("RetailMembers");

                MemberGroup mg = MemberGroup.GetByName("Retail"); //error here

                if (Member.GetMemberFromLoginName(txtUsername.Text) == null && Member.GetMemberFromEmail(txtEmail.Text) == null)
                {

                    //create a member

                    //Member m = Member.MakeNew(tbFirstName.Text + " " + tbLastName.Text, mt, new umbraco.BusinessLogic.User(tbFirstName.Text + tbLastName.Text, tbPassword.Text));

                    Member m = Member.MakeNew(txtUsername.Text,txtEmail.Text, mt, new umbraco.BusinessLogic.User(0));

                    m.getProperty("MemberGroup").Value = "Retail"; //error here

                 
                    //Generate member Xml Cache

                    m.XmlGenerate(new System.Xml.XmlDocument());



                    //Save member

                    m.Save();

                    Label1.Text = mt.Alias.ToString();


                }
                else
                {
                    if (Member.GetMemberFromLoginName(txtUsername.Text) != null)
                    {
                        txtUsername.Attributes["onfocus"] = "this.select();";
                        rfvUsername.Attributes["class"] = "fieldError";
                        rfvUsername.IsValid = false;
                    }
                    else if (Member.GetMemberFromEmail(txtEmail.Text) != null)
                    {
                        rfvtxtEmail.IsValid = false;
                        rfvtxtEmail.ErrorMessage = "This email is already registered to another account. Please use another email address.";
                    }
                  
                }

  • Harald Ulriksen 207 posts 249 karma points
    Mar 15, 2011 @ 13:02
    Harald Ulriksen
    0

    Hi,

    assuming you are using a fairly recent version of umbraco, 4.x, I suggest that you use the standard ASP.Net api for this

    System.Web.Security.MembershipUser membershipUser = System.Web.Security.Membership.CreateUser(username, password, email);
    System.Web.Security.Roles.AddUserToRole(username, "AnUmbracoGroupName");


    Se also this post from @slace / Aaron Powell on member profile http://www.aaron-powell.com/umbraco-members-profiles

    Hope this helps,
    Harald

     

  • Sean 141 posts 179 karma points
    Mar 16, 2011 @ 08:43
    Sean
    0

    Hi Harald,

    Thanks for the answer. How would I do it using the code above? I missing a line or something.  Am I able to use the code below to add this member to a certain group?

     

     MemberType mt = MemberType.GetByAlias("RetailMembers");

    mt.add // or something similiar?

  • Harald Ulriksen 207 posts 249 karma points
    Mar 16, 2011 @ 09:12
    Harald Ulriksen
    0

    Hi Sean,

    it should be straight forward - unless you have multiple membertypes. Remember to set default membertyp in web.config even if you only have one type.

    Here's some code I used to do a import of members from a csv file.

    if (System.Web.Security.Membership.GetUser(username) == null)
     {
     
    System.Web.Security.MembershipUser membershipUser = System.Web.Security.Membership.CreateUser(username, password, email);
     System.Web.Security.Roles.AddUserToRole(username, "Retail");
     var profile = (MemberProfile)System.Web.Profile.ProfileBase.Create(username);
     profile.Branch = branchList.Single(pv => pv.Value == franchise).Id.ToString();
     profile.FirstName = userRecord["Fornavn"];
     profile.LastName = userRecord["Etternavn"];
     profile.Save();
     importCount++;
     }

    Memberprofile class

        public class MemberProfile : ProfileBase
        {

            [SettingsAllowAnonymous(false)]
            public string Branch
            {
                get
                {
                    var o = base.GetPropertyValue("branch");
                    if (o == DBNull.Value)
                    { return string.Empty; }
                    return (string)o;
                }
                set { base.SetPropertyValue("branch", value); }
            }

            [SettingsAllowAnonymous(false)]
            public string FirstName
            {
                get
                {
                    var o = base.GetPropertyValue("firstname");
                    if (o == DBNull.Value)
                    { return string.Empty; }
                    return (string)o;
                }
                set { base.SetPropertyValue("firstname", value); }
            }
            [SettingsAllowAnonymous(false)]
            public string LastName
            {
                get
                {
                    var o = base.GetPropertyValue("lastname");
                    if (o == DBNull.Value)
                    { return string.Empty; }
                    return (string)o;
                }
                set { base.SetPropertyValue("lastname", value); }
            }
        }


    If you have multiple membertypes and need to target a specific membertype, you must use the umbraco api to create the member. The member can still be added to the appropriate group using the AddUserToRole method.





  • Sean 141 posts 179 karma points
    Mar 18, 2011 @ 02:53
    Sean
    0

    Hi Harald,

    Thanks for getting back to me and post that code, it's really helpful. I have a question regarding what I need to add into the web.config file? Do I need to add each property in? as in firstname, lastname etc? or do I just add the classname? I'm not really sure on this part, are you able to clarfiy this for me please? If you have a snippet or something so I can get my head around it?

    Thanks in advance,

    Sean

  • Harald Ulriksen 207 posts 249 karma points
    Mar 18, 2011 @ 05:22
    Harald Ulriksen
    0

    The setting in web config is just the name of the Group.

    Under system.web find the add node with name="UmbracoMembershipProvider". Make sure that you set the defaultMemberTypeAlias="Another Type" to the name of an existing member group in umbraco.

    h.

  • Sean 141 posts 179 karma points
    Mar 19, 2011 @ 06:23
    Sean
    0
Please Sign in or register to post replies

Write your reply to:

Draft