Just recently an issue was brought to my attention that my External Database Provider module when creating users was making it so that notification e-mails could not be sent when assigning roles. So I started looking into the code, and how I was creating a user. The user creation code was something that I have been using for a long time, some code that was first found on a blog or forum post about 2-3 years ago. Not noticing anything wrong with the code, at least from an obvious point of view I started to look into the database to see if there were any differences at the database level. Looking into the users' information I noticed that they were missing their user profile, and then I found out that a few key elements still needed to be set to "fully" create the user. This posting shows you the "Full Code" to insert a user into a DotNetNuke portal from a C# Codebehind, translation into VB should be very simple.
The most logical starting point here is to share the code. THe following code snippet shows the proper way to create a DotNetNuke user, with full integration to ensure that no errors will be encountered.
1: UserInfo oUser = new UserInfo();
2: oUser.PortalID = this.PortalId;
3: oUser.IsSuperUser = false;
4: oUser.FirstName = "FirstName";
5: oUser.LastName = "LastName";
6: oUser.Email = "[email protected]";
7: oUser.Username = "MyUsername";
8: oUser.DisplayName = "FirstName LastName";
10: //Fill MINIMUM Profile Items (KEY PIECE)
11: oUser.Profile.PreferredLocale = PortalSettings.DefaultLanguage;
12: oUser.Profile.TimeZone = PortalSettings.TimeZoneOffset;
13: oUser.Profile.FirstName = oUser.FirstName;
14: oUser.Profile.LastName = oUser.LastName;
16: //Set Membership
17: UserMembership oNewMembership = new UserMembership();
18: oNewMembership.Approved = true;
19: oNewMembership.CreatedDate = System.DateTime.Now;
20: oNewMembership.Email = oUser.Email;
21: oNewMembership.IsOnLine = false;
22: oNewMembership.Username = oUser.Username;
23: oNewMembership.Password = oExternalUserAccount.Password;
25: //Bind membership to user
26: oUser.Membership = oNewMembership;
28: //Add the user, ensure it was successful
29: if (UserCreateStatus.Success == UserController.CreateUser(ref oUser))
31: //Take Success Action
As you can see from this were have three distinct actions that we perform before we actually create the users account. The first step is to declare the user object and fill the common informaiton such as name, display name, and email address. We then progress to populating the profile, this is the key piece missing from many of the examples. We set the locale and timezone to match the portal default, this ensures that all localization and other features work for the user. Lastly we configure their membership information, including password.
Once the users information has been properly configured we call the CreateUser method of the UserController to actually create the user and check the creation status for errors. A very simple code example, but something that is not always straightforward to work with.
I hope this has helped someone else looking to create users in code from DotNetNuke. Please share any comments below, for assistance please post to the forum.