Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeArchived Discus...Archived Discus...Developing Under Previous Versions of .NETDeveloping Under Previous Versions of .NETASP.Net 2.0ASP.Net 2.0HOW TO: Programmatically Create New UserHOW TO: Programmatically Create New User
Previous
 
Next
New Post
5/29/2007 5:13 PM
 

I am needing to create a new user programatically outside of the ManageUsers control.  While stepping through the code, I believe the following might do what I need to do, but I was wondering if anyone out there might be able to confirm what I have done.  Here is a snippet:

Public Function CreateNewUser(ByVal strUsername As String, ByVal strFirstName As String, ByVal strLastName As String, _
ByVal strEmailAddress As String) As DotNetNuke.Security.Membership.UserCreateStatus
' create a new user instance and populate it
Me.User = New DotNetNuke.Entities.Users.UserInfo
With Me.User
.DisplayName = String.Concat(strFirstName, " ", strLastName)
.Email = strEmailAddress
.FirstName = strFirstName
.IsSuperUser = False
.Membership.CreatedDate = DateTime.Now
.Membership.Email = strEmailAddress
.Membership.Password = DotNetNuke.Entities.Users.UserController.GeneratePassword
.Membership.UpdatePassword = True
.Membership.Username = strUsername
.Username = strUsername
End With
'Set the Approved status based on the Portal Settings
If Me.PortalSettings.UserRegistration = DotNetNuke.Common.Globals.PortalRegistrationType.PublicRegistration Then
Me.User.Membership.Approved = True
Else
Me.User.Membership.Approved = False
End If
' attempt to create the DNN user
Dim objStatus As UserCreateStatus = Users.UserController.CreateUser(Me.User)
Dim args As Modules.UserUserControlBase.UserCreatedEventArgs
If objStatus = UserCreateStatus.Success Then
args = New Modules.UserUserControlBase.UserCreatedEventArgs(User)
args.Notify = True
Else ' registration error
args = New Modules.UserUserControlBase.UserCreatedEventArgs(Nothing)
End If
args.CreateStatus = objStatus
' perform some DNN logic
Dim objUserBase As New Modules.UserUserControlBase
objUserBase.OnUserCreated(args)
objUserBase.OnUserCreateCompleted(args)
Select Case objStatus
Case UserCreateStatus.Success
' add to another (separate) membership
If Me.User.UserID > 0 Then MYNAMESPACE.Events.UserLogin(Me.User)
Case Else
' something happened
End Select
Return objStatus
End Function

Can anyone help me?  I have just stepped through a lot of the source code to figure this much out and just would appreciate another set of eyes for a minute.  :)


Will Strohl

Upendo Ventures Upendo Ventures
DNN experts since 2003
Official provider of the Hotcakes Commerce Cloud and SLA support
 
New Post
5/30/2007 11:43 AM
 

You are missing something very important... portalid.  I have attached a small portion from an import module I wrote to pull data from an HR file and you'll see that we are pretty much on the same lines.  Please note that the "else" statement below comes right after a check to see if the user already exists (in a hashtable I loaded with portal users).  Lines in red are important for various reasons... doesn't look like you are trying to do anything with profile fields, but this is included just in case.

 else
{
user = new UserInfo();
user.Profile.InitialiseProfile(PortalId);
user.Email = rec.Email;
user.PortalID = PortalId;
user.Username = rec.Login;
user.Profile.PreferredLocale = "en-US";
// strip off leading 0's
user.Profile.SetProfileProperty("Employee ID", rec.EmployeeId.TrimStart(new char[] { '0' }));
user.Membership.Password = UserController.GeneratePassword(9);//Macu.Security.PasswordGenerator.GeneratePassword();
user.Membership.Username = rec.Login;
}

user.FirstName = rec.FirstName;
user.LastName = rec.LastName;
user.DisplayName = rec.DisplayName;

// set profile
user.Profile.FirstName = rec.FirstName;
user.Profile.LastName = rec.LastName;
user.Profile.SetProfileProperty("Date of Birth", rec.DateOfBirth.ToShortDateString());
user.Profile.SetProfileProperty("Seniority Date", rec.HireDate.ToShortDateString());
user.Profile.SetProfileProperty("Work Phone", rec.WorkPhone);
// strip off leading 0's
user.Profile.SetProfileProperty("Supervisor ID", rec.SupervisorId.TrimStart(new char[] { '0' }));
user.Profile.SetProfileProperty("Employee Status", rec.Status);
// get old code, if any
previousLoc = user.Profile.GetPropertyValue("Org Level 2 ID");
user.Profile.SetProfileProperty("Org Level 2 ID", rec.OrgLevel2Code);
user.Profile.SetProfileProperty("Position", rec.Position);

// set membership
user.Membership.Email = rec.Email;
user.Membership.Approved = (rec.Status == "A" || rec.Status == "L");


-- Jon Seeley
DotNetNuke Modules
Custom DotNetNuke and .NET Development
http://www.seeleyware.com
 
New Post
5/30/2007 11:57 AM
 

What great timing!  :)  I was literally about to test this...  Really...  I was waiting for the rebuild to finish.  :)

I completely forgot about the profile settings (and didn't know about the portal spec/init).  Right now, this method is creating some barebones user accounts, so most of the fields aren't necessary to know just yet. 

I really appreciate the help.  Thank you so much! 


Will Strohl

Upendo Ventures Upendo Ventures
DNN experts since 2003
Official provider of the Hotcakes Commerce Cloud and SLA support
 
New Post
5/31/2007 4:50 PM
 

I am getting a foreign key constraint error.  The error is:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_UserProfile_Users". The conflict occurred in database "DATABASENAME", table "dbo.Users", column 'UserID'. The statement has been terminated.

The user record gets created in the Users table, but the profile record is not added in the UserProfile table.  Here is an example of the code I am using:

Public Function CreateNewUser(ByVal strUsername As String, ByVal strFirstName As String, ByVal strLastName As String, _
     ByVal strEmailAddress As String) As DotNetNuke.Security.Membership.UserCreateStatus
     ' preliminary check to be sure that the fields are filled in
     If String.IsNullOrEmpty(strUsername) Or String.IsNullOrEmpty(strFirstName) Or _
          String.IsNullOrEmpty(strLastName) Or String.IsNullOrEmpty(strEmailAddress) Then
               ' cannot have empty values
               If String.IsNullOrEmpty(strUsername) Then _
                    Throw New NullReferenceException("The USERNAME variable for CreateNewUser() cannot be empty.")
               If String.IsNullOrEmpty(strFirstName) Then _
                    Throw New NullReferenceException("The FIRSTNAME variable for CreateNewUser() cannot be empty.")
               If String.IsNullOrEmpty(strLastName) Then _
                    Throw New NullReferenceException("The LASTNAME variable for CreateNewUser() cannot be empty.")
               If String.IsNullOrEmpty(strEmailAddress) Then _
                    Throw New NullReferenceException("The EMAILADDRESS variable for CreateNewUser() cannot be empty.")
     Else
          ' validate the e-mail address format
          If Not MyNameSpace.Common.Utilities.RegExLibrary.ValidateEmailFormat(strEmailAddress) Then _
               Throw New Exception("The e-mail address was in an invalid format")
     End If
     ' create a new user instance and populate it
     Me.User = New DotNetNuke.Entities.Users.UserInfo
     With Me.User
          .Profile.InitialiseProfile(PortalSettings.PortalId)
          .DisplayName = String.Concat(strFirstName, " ", strLastName)
          .Email = strEmailAddress
          .FirstName = strFirstName
          .IsSuperUser = False
          .Membership.CreatedDate = DateTime.Now
          .Membership.Email = strEmailAddress
          .Membership.Password = DotNetNuke.Entities.Users.UserController.GeneratePassword
          .Membership.UpdatePassword = True
          .Membership.Username = strUsername
          .Username = strUsername
          .Profile.FirstName = strFirstName
          .Profile.LastName = strLastName
          .Profile.PreferredLocale = Me.PortalSettings.DefaultLanguage
          .Profile.TimeZone = Me.PortalSettings.TimeZoneOffset
     End With
     'Set the Approved status based on the Portal Settings
     If Me.PortalSettings.UserRegistration = DotNetNuke.Common.Globals.PortalRegistrationType.PublicRegistration Then
          Me.User.Membership.Approved = True
     Else
          Me.User.Membership.Approved = False
     End If
     ' attempt to create the DNN user
     Dim objStatus As UserCreateStatus = Users.UserController.CreateUser(Me.User)
     Dim args As Modules.UserUserControlBase.UserCreatedEventArgs
     If objStatus = UserCreateStatus.Success Then
          args = New Modules.UserUserControlBase.UserCreatedEventArgs(Me.User)
          args.Notify = True
     Else    ' registration error
          args = New Modules.UserUserControlBase.UserCreatedEventArgs(Nothing)
          args.Notify = False
     End If
     args.CreateStatus = objStatus
     ' perform some DNN logic
     Me.UserCreateCompleted(args)
     Select Case objStatus
          Case UserCreateStatus.Success
               ' add to the BLL membership
               Me.User = DotNetNuke.Entities.Users.UserController.GetUserByName(PortalSettings.PortalId, Me.User.Username, True)
               HttpContext.Current.Session.Add("CreateNewUserID", Me.User.UserID)
               If Me.User.UserID > 0 Then MyNameSpace.Events.UserLogin(Me.User)
          Case Else
               ' something happened
     End Select
     Return objStatus
End Function

I thought about moving the Profile assignments lower, but I do not know how to save changes to the userinfo object yet (I haven't researched it yet, but it is probably somewhere in the ManageUsers.ascx control in the DNN source).  What is causing this error and how do I get around it?


Will Strohl

Upendo Ventures Upendo Ventures
DNN experts since 2003
Official provider of the Hotcakes Commerce Cloud and SLA support
 
New Post
6/5/2007 4:21 PM
 

This is really strange... I can't see anything in the code that would/could cause the error... to save changes to an existing user, you would use the UserController.UpdateUser(portalid, user), but everything up above suggests that Me.User is a brand new instance of an object, thus has no userid at all. 

You are doing the exact same thing with yours that I am doing already, so I honestly don't know why it wouldn't save to the profile table... except if for some reason it cannot get the userid from the request, but that doesn't really make sense either.


-- Jon Seeley
DotNetNuke Modules
Custom DotNetNuke and .NET Development
http://www.seeleyware.com
 
Previous
 
Next
HomeHomeArchived Discus...Archived Discus...Developing Under Previous Versions of .NETDeveloping Under Previous Versions of .NETASP.Net 2.0ASP.Net 2.0HOW TO: Programmatically Create New UserHOW TO: Programmatically Create New User


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out