Nibble

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.

image

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.

image

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

40 Comments so far

  1. Neil on April 24th, 2008

    Nice work, but is there no way to instantiate MemberType by its alias name?
    Seems a shame.

  2. Tim Geyssens on April 24th, 2008

    Hi Neil,

    I’ll have a look, should be possible.

    Regards,
    Tim

  3. Tim Geyssens on April 24th, 2008

    You can get the MemberType by its alias like this:

    MemberType demoMemberType = MemberType.GetByAlias(”demo”);

  4. Biagio Paruolo on April 28th, 2008

    And group?

  5. Tim Geyssens on April 28th, 2008

    Group is possible, I’ll check it and update the post.

  6. Rachid Zyad on May 19th, 2008

    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

  7. Rachid Zyad on May 19th, 2008

    Never mind, I got it done. I just use the method “AddGroup(GroupNodeId)” for the object NewMember.

    Thanks

  8. Tim Geyssens on May 19th, 2008

    Great ! yeah just explorer the api a bit and you will notice it is all very easy.

  9. Rachid Zyad on May 19th, 2008

    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

  10. Tim Geyssens on May 20th, 2008

    I’ll give it a try and get back to you

  11. Rachid Zyad on May 20th, 2008

    I got it fixed (I was using the wrong web config file).
    Thanks

  12. Rachid Zyad on May 22nd, 2008

    anybody know from where I can download UltraSimpleMailer2.zip

  13. Biagio Paruolo on July 3rd, 2008

    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?

  14. Tim Geyssens on July 3rd, 2008

    @Biagio, download umbraco and you can find the dll files in the bin directory. Just reference those.

  15. Thomas.R on August 20th, 2008

    Thanks you so much for this short demo

  16. Tarek Faham on November 3rd, 2008

    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.

  17. Troels on November 3rd, 2008

    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 ? ?

  18. Nikola Petkovic on February 19th, 2009

    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!

  19. Antonio Costa on March 16th, 2009

    Hi.

    It’s possible to create users programatically, instead of members?

    Tanks

  20. Antonio Costa on March 20th, 2009

    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

  21. Dave on April 16th, 2009

    member.AddGroup(int ID)

    Umbraco 4 front end, as far as I can tell, the IDs are string?!

    Any clues???

  22. Jonas Eriksson on May 22nd, 2009

    The same issue Nikola mentioned happened to me too, and the solution worked (also the Api cheatsheet tells us to do a XmlGenerate).

  23. William on December 30th, 2009

    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

  24. William on December 30th, 2009

    Ops, the code for the macro i´m looking for:
    (umbraco:Macro DocumentId=”[#pageID]” PropertyName=”Conter” NewValue=”Valor/XLST” xslt=”0″ /)

  25. Arthur on January 14th, 2010

    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

  26. Jesper on March 23rd, 2010

    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’

  27. Tim Geyssens on March 31st, 2010

    try leaving out the new and just do

    MemberType demoMemberType = MemberType.GetByAlias(”Demo”);

  28. Jesper on May 1st, 2010

    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);
    }
    }
    }

  29. vijay on August 6th, 2010

    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

  30. Chris Lord on January 29th, 2011

    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

  31. Owen Tang on April 15th, 2011

    “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.

  32. dashidammy on August 28th, 2011

    Security Companies in London

  33. Bassem on September 8th, 2011

    working great , thanks ;)

  34. Tom on October 11th, 2011

    Very useful = cheers

  35. jose on February 8th, 2012

    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

  36. rohit on February 29th, 2012

    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);
    ….
    }

  37. punji on March 6th, 2012

    Too Good

  38. parvendra on August 7th, 2012

    I want to make new member by a different asp.net app by using Umbraco membership dlls.
    is it possible?

  39. Vasiliy on December 16th, 2013

    Thanks!

  40. I used to ƅe recommended this website ᴠia my
    cousin. I’m noᴡ not surе whether or not this publish is written by means of Һim
    as nobody elѕe know such distinctivе approximately my difficulty.
    You аre incrediЬle! Thank you!

Leave a Reply