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 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?
[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.
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?
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.
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.";
}
}
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
Se also this post from @slace / Aaron Powell on member profile http://www.aaron-powell.com/umbraco-members-profiles
Hope this helps,
Harald
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?
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.
Memberprofile class
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.
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
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.
is working on a reply...