Back to all posts

Creating a DotNetNuke User Account From Code

Posted on Dec 22, 2008

Posted in category:
Development
DNN

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# code-behind, translation into VB should be very simple.

The Code

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.

Creating a DNN User via C#
UserInfo oUser = new UserInfo();
oUser.PortalID = this.PortalId;
oUser.IsSuperUser = false;
oUser.FirstName = "FirstName";
oUser.LastName = "LastName";
oUser.Email = "[email protected]";
oUser.Username = "MyUsername";
oUser.DisplayName = "FirstName LastName";

//Fill MINIMUM Profile Items (KEY PIECE)
oUser.Profile.PreferredLocale = PortalSettings.DefaultLanguage;
oUser.Profile.TimeZone = PortalSettings.TimeZoneOffset;
oUser.Profile.FirstName = oUser.FirstName;
oUser.Profile.LastName = oUser.LastName;

//Set Membership
UserMembership oNewMembership = new UserMembership();
oNewMembership.Approved = true;
oNewMembership.CreatedDate = System.DateTime.Now;
oNewMembership.Email = oUser.Email;
oNewMembership.IsOnLine = false;
oNewMembership.Username = oUser.Username;
oNewMembership.Password = oExternalUserAccount.Password;

//Bind membership to user
oUser.Membership = oNewMembership;

//Add the user, ensure it was successful
if (UserCreateStatus.Success == UserController.CreateUser(ref oUser))
{
    //Take Success Action
}

As you can see from this were have three distinct actions that we perform before we actually create the user's account. The first step is to declare the user object and fill the common information 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 user's 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.

Summary

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.