Creating members programmatically
Maybe you want to import a number of members or create a custom registration form. You can use the umbraco api to create, update and delete members
I’ll first create a ‘demo’ member type and add 2 properties.
Address (alias address) and City (alias city) both of the type textstring.
Owkey now I am ready to write some c# code that will create a new member.
Before I can access the umbraco member api I need to reference cms.dll and businesslogic.dll (these can be found in the bin folder of umbraco).
Now I’ll add a new usercontrol to my project where a member can register. With a couple of textboxes and a button.
So the code I need to execute on the click event of the button is:
first add this at the top
using umbraco.cms.businesslogic.member;
using umbraco.cms.businesslogic.propertytype;
And on the click event:
if (Member.GetMemberFromEmail(txtEmail.Text) == null)
{
MemberType demoMemberType = new MemberType(1040); //id of membertype ‘demo’
Member newMember = Member.MakeNew(txtName.Text, demoMemberType, new umbraco.BusinessLogic.User(0));
newMember.Email = txtEmail.Text;
newMember.Password = txtPassword.Text;
newMember.LoginName = txtUsername.Text;
newMember.getProperty(“address”).Value = txtAddress.Text; //set value of property with alias ‘address’
newMember.getProperty(“city”).Value = txtCity.Text; //set value of property with alias ‘city’
newMember.Save();
}
else
{
//member exists
}
First I check if there is no other member with the email address, if the email address is not used yet I can make a new member.
Demo project: download
Nice work, but is there no way to instantiate MemberType by its alias name?
Seems a shame.
Hi Neil,
I’ll have a look, should be possible.
Regards,
Tim
You can get the MemberType by its alias like this:
MemberType demoMemberType = MemberType.GetByAlias(”demo”);
And group?
Group is possible, I’ll check it and update the post.
I just started using umbraco for 4 weeks. Good work guys!. I used this code to create a new membership but it didn’t assign a member group to it. Thanks
Never mind, I got it done. I just use the method “AddGroup(GroupNodeId)” for the object NewMember.
Thanks
Great ! yeah just explorer the api a bit and you will notice it is all very easy.
When I used this following code to return all member-groups:
foreach (umbraco.cms.businesslogic.member.MemberGroup mg in umbraco.cms.businesslogic.member.MemberGroup.GetAll)
{
ddlMemberGroup.Items.Add(new ListItem(mg.Text, mg.Id.ToString()));
}
I got this error of the connection string: The ConnectionString property has not been initialized.
Any ideas, Thanks
I’ll give it a try and get back to you
I got it fixed (I was using the wrong web config file).
Thanks
anybody know from where I can download UltraSimpleMailer2.zip
Is there a method for retrieving thh GroupNodeId?
How setup VS2008 for develop Umbraco Addon o Extend it? ( I only copy Umbraco bin file in my solution o reference bin files ?
What are dll files?
@Biagio, download umbraco and you can find the dll files in the bin directory. Just reference those.
Thanks you so much for this short demo
Nice work. Is there a way to develop a user control that will perform the authentication based on Windows Active Directory, or the Current LOGON_USER ?
I want use the LOGON_USER server variable to retrieve the currently logged on user, and have this user authenticated under umbraco. How ? ie, only User ID, and No Password.
Tarek.
Hey mate greate site!
currently i am working on a newsletter module, and want to use umbracos members and membertypes as subscribers and mailing lists.. but the problem is that a member type and member dont see to have that mucht in common
so atm i am using SQL to get the members in a group. and it works great.. but then comes the properties of the group.. i cant refer to them so again i made a small hax where i convert that into a Umbraco Document and that way i can get acess to the properties… do u know if there is a better connection between members and memberTypes that i havnt stumpled upon yet ? ?
Hi Tim,
I tried this on umbraco v4 but i faced some issues.
Once a member is created this way, the record is inserted into umbracoNode table, but not in cmsContentXml table which didnt allow me to get the current user immediately by xslt. (umbraco.library.GetCurrentMember() threw an exception and umbraco.library.GetMember(newMember.Id) couldnt find it)
The solution I came up with was that you have to add the line
newMember.XmlGenerate(new System.Xml.XmlDocument());
before or after saving newMember.
Can you please check if this comment is of any worth?
Cheers!
Hi.
It’s possible to create users programatically, instead of members?
Tanks
well, I just see one way to that.
You can create an usercontrol that insert new user in the table umbracoUser and what sections he have access in umbracoUser2app.
If you have an easiest way, please refere it.
Thanks
member.AddGroup(int ID)
Umbraco 4 front end, as far as I can tell, the IDs are string?!
Any clues???
The same issue Nikola mentioned happened to me too, and the solution worked (also the Api cheatsheet tells us to do a XmlGenerate).
How do I create a macro which alters a document propertie?
I´m looking for something like that:
Or something like that.
Is it possible only using xslt? Or only with UC?
Thanks! And this blog is awesome! Really helpful
Ops, the code for the macro i´m looking for:
(umbraco:Macro DocumentId=”[#pageID]” PropertyName=”Conter” NewValue=”Valor/XLST” xslt=”0″ /)
I am trying to create new members using the Umbraco API. Here is my code.
if (Member.GetMemberFromEmail(”an_user@gmail.com”) == null
)
{
MemberType memberType = MemberType.GetByAlias(”AdminType”);
MemberGroup mg = MemberGroup.GetByName(”AdminGroup”);
Member newMember = Member.MakeNew(”Email”, memberType, new umbraco.BusinessLogic.User(0));
newMember.Email = ”
an_user@gmail.com“;
newMember.Password =
“Password”;
newMember.LoginName =
an_user@gmail.com;
newMember.getProperty(
“Address”).Value = “Address”;
//set value of property with alias address
newMember.getProperty(
“Phone”).Value = “Phone”;
//set value of property with alias city
//Generate member Xml Cache
//newMember.XmlGenerate(new System.Xml.XmlDocument());
newMember.AddGroup(mg.Id);
newMember.Save();
}
else
{
//member exists
}
The above code is throwing an exception on the first line (Member.GetMemberFromEmail(an_user@gmail.com) )
I do know that Member.GetMemberFromEmail will return null if the email address doesn’t exists. My problem is, the exception is being thrown from inside the GetmemberFromEmail method .
(When i commented that line and ran the program , the application threw the null reference exception from the next line MemberType memberType = MemberType.GetByAlias(”AdminType”); )
Does anyone have any insight on the cause of this issue ?
I would appreciate any guidance to solve this problem.
Thanks
I have the same problem.
MemberType demoMemberType = new MemberType.GetByAlias(”Demo”);
‘umbraco.cms.businesslogic.member.MemberType.GetByAlias(string)’ is a ‘method’ but is used like a ‘type’
try leaving out the new and just do
MemberType demoMemberType = MemberType.GetByAlias(”Demo”);
Thanks Tim. That works
Another problem. How do I sort members by memberGroup??
I need to show members that are in one membergroup but I can’t make it work.
I have tried this, but it doesn’t work:
Member[] member = Member.GetAll;
MemberGroup memberGroup = MemberGroup.GetByName(”BasicUser”);
foreach(Member m in member)
{
foreach(Member mg in m)
{
if(mg.ToString().Equals(m.Groups))
{
newUserList.Items.Add(m.LoginName);
}
}
}
Hi,
I am trying to dynamically create new members with an asp .net CreateUserWizard user control in Umbraco 4.5. The problem I have is that I am unable to save the custom property values that I had added to my member type. It does save the username, email address, and password correctly.
I noticed that the Member.MakeNew method is obsolete. It says to use the System.Web.Security.Membership.CreateUser. If I use this method, how do I set the values for my custom property values of my member type?
Thanks
Protected Sub CreateUserWizard1_CreatedUser(ByVal sender As Object, ByVal e As EventArgs) Handles CreateUserWizard1.CreatedUser
Dim lMemberType As MemberType
Dim lNewMember As Member
If (Membership.GetUserNameByEmail(CreateUserWizard1.Email) Is Nothing) Then
lMemberType = MemberType.GetByAlias(”RegisteredUser”)
lNewMember = Member.MakeNew(CreateUserWizard1.UserName, lMemberType, New BusinessLogic.User(0))
‘lNewMember = Membership.CreateUser(CreateUserWizard1.UserName, CreateUserWizard1.Password, CreateUserWizard1.Email)
lNewMember.Email = CreateUserWizard1.Email
lNewMember.Password = CreateUserWizard1.Password
lNewMember.LoginName = CreateUserWizard1.UserName
Dim lFirstName As cms.businesslogic.property.Property = lNewMember.getProperty(”fname”)
Dim lFirstNameTextBox As TextBox = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl(”FirstName”), TextBox)
lFirstName.Value = lFirstNameTextBox.Text
Dim lLastName As cms.businesslogic.property.Property = lNewMember.getProperty(”lname”)
Dim lLastNameTextBox As TextBox = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl(”LastName”), TextBox)
lLastName.Value = lLastNameTextBox.Text
‘lNewMember.getProperty(”fname”).Value = “Jeremy”
‘lNewMember.getProperty(”lname”).Value = “Vo”
lNewMember.AddGroup(MemberGroup.GetByName(”Registered Users”).Id)
lNewMember.Save()
End If
End Sub
Thanks for the article, found this really useful. Using your example, I am able to create my member, set property values and add to a default group. However, when I do a search for my new member in the admin section, I get a “The given key was not present in the dictionary.” error. If i go into my member using the alpahbetical list, and then save it, it works ok….weird, any ideas what this might be?
thanks
Chris
“When I do a search for my new member in the admin section, I get a “The given key was not present in the dictionary.” error. If i go into my member using the alpahbetical list, and then save it, it works ok…”
Hi, Tim. I have the same problem.
Security Companies in London
working great , thanks
Very useful = cheers
there is a way to create multiple members? from the same web form? for example if we have three fieldset in the web form should create 3 members
Hi
i got this error while
The type initializer for ‘umbraco.cms.businesslogic.datatype.controls.Factory’ threw an exception.
the error on code is
{
…
Member newMember = Member.MakeNew(txtName.Text, MemberType.GetByAlias(”DOC_AdSpace”), user);
….
}
Too Good
I want to make new member by a different asp.net app by using Umbraco membership dlls.
is it possible?
Thanks!
First off I would like to say terrific blog! I had a quick question which I’d like
to ask if you do not mind. I was interested
to find out how you center yourself and clear your thoughts prior to writing.
I have had trouble clearing my thoughts in getting my ideas out there.
I truly do take pleasure in writing however it just seems like the first 10 to 15 minutes tend to be lost just trying to figure out
how to begin. Any recommendations or hints? Appreciate it!
Unquestionably imagine that that you said. Your favorite justification seemed to be
at the net the simplest factor to take into account of.
I say to you, I definitely get annoyed at the same time as other
folks think about issues that they plainly don’t realize about.
You managed to hit the nail upon the highest as well
as outlined out the whole thing with no need side-effects ,
folks can take a signal. Will probably be again to get more.
Thanks