Sunday, 8 May 2011

Create User Programmatically in DotNetNuke

One colleague asked about example on how to create user in DotNetNuke programmatically. Here is simple example on C# how to do this:


private void AddUser()
{
 DotNetNuke.Entities.Users.UserInfo objUser =
new DotNetNuke.Entities.Users.UserInfo();
 objUser.AffiliateID = Null.NullInteger;
 objUser.Email =
"user@email.com";
 objUser.FirstName =
"FirstName";
 objUser.IsSuperUser =
false;
 //I bet you will not create SuperUsers in bulk ;)

 objUser.LastName =
"LastName";
 objUser.PortalID =
this.PortalId;
 objUser.Username =
"UserName";
 objUser.DisplayName =
"Display Name";
 
//Usually here FirstName+LastName

 objUser.Membership.Password =
"your_password";
 //please check in web.config requirements for your password (length, letters, etc)
 objUser.Membership.Approved =
true;
 objUser.Membership.Email =
"user@email.com";
 objUser.Membership.Username = objUser.Username;
 objUser.Membership.UpdatePassword =
true;
 //this one needs if you want user to update password on first login, else set to false
 objUser.Profile.Country =
"Your Country";
 objUser.Profile.Street =
"Street";
 objUser.Profile.City =
"City";
 objUser.Profile.Region =
"Region";
 objUser.Profile.PostalCode =
"PostalCode";
 objUser.Profile.Unit =
"Unit";
 objUser.Profile.Telephone =
"Telephone";
 objUser.Profile.FirstName = objUser.FirstName;
 objUser.Profile.LastName = objUser.LastName;
 DotNetNuke.Security.Membership.UserCreateStatus objCreateStatus =
 DotNetNuke.Entities.Users.UserController.CreateUser(
ref objUser);
 if (objCreateStatus == DotNetNuke.Security.Membership.UserCreateStatus.Success)
 {
   
//everything fine
 }
 else
 {
   
//simple show error
   lblError.Text = objCreateStatus.ToString();
 }
}


Hope this helps!

9 comments:

  1. greetings,
    I am getting an obsolete warning when adding in objUser.Membership.Username = objUser.Username; in DNN 6. Do you know the new way in DNN6?
    Thanks!
    Jay Stratemeyer

    ReplyDelete
    Replies
    1. objUser.Membership = new UserMembership(objUser);

      This sets those properties and others that were deprecated, within the constructor of UserMembership

      Delete
  2. Hi,

    I'm getting a couple errors that say that the name "Null", doesn't exist in the current context, and lblErrors doesn't exist in the current context either. If you could help that would be great!
    Thank You!
    Turner Bell

    ReplyDelete
  3. Null goes from the DotNetNuke.Common.Utilites namespace. lblErrors - just a asp:Label on the form. Hope this helps.

    ReplyDelete
  4. thanks alot , you really helped me a lot :*

    ReplyDelete
  5. Hi,
    I am using above code to create user and profile.
    User created successfully. when i get user info by id, i do not get profile data.
    I checked userprofile table there data store enter by me.

    I am not sure why profile info not come on get user.

    Kindly let me know how to fix it.

    Regards,
    Vikash

    ReplyDelete

Note: only a member of this blog may post a comment.