Thursday, February 09, 2012   Click here to search  
Welcome: Register | Login
You Are Here » Forums
 
Orlando DotNetNuke® Users Group (ODUG) Forums
 
 
HomeHomeGeneralGeneralHow To, Tips & ...How To, Tips & ...HOW TO: Programatically Create A New User in DotNetNuke®HOW TO: Programatically Create A New User in DotNetNuke®
Previous
 
Next
New Post
7/7/2008 1:09 PM
 

Originally Published at:  http://blog.strohlsitedesign.com/Blog/tabid/66/EntryID/59/Default.aspx

I have come to realize that many people have the need in their DotNetNuke® (DNN) applications to create a new user "on-the-fly".  Here is how to do it...

First of all, thank you to those who helped me troubleshoot this in the DotNetNuke® forums.

You obviously will want to create your own class and take advantage of the Membership, Profile, and Role providers that are in place.  In order to effectively debug this code, I found it necessary to create duplicates of those providers.  Here is the finalized code snippet that I know to work in DNN v4.05.01 through v4.05.03:

'
' Put the following method into your own class
' This method assumes that you have these local properties:
' "User" As DotNetNuke.Entities.Users.UserInfo
' "PortalSettings" As DotNetNuke.Entities.Portals.PortalSettings
'
' IMPORTANT - There are extra line breaks added using the VB _
' to prevent breaking the design.
'
Public Function CreateNewUser(ByVal strUsername As String, _
     ByVal strFirstName As String, ByVal strLastName As String, _
     ByVal strEmailAddress As String, ByVal blnMembershipApproved _
     As Boolean) As DotNetNuke.Security.Membership.UserCreateStatus
     '===========================================================
     '
     ' BEGIN VALIDATION
     '
     '===========================================================
     ' preliminary check to be sure that the fields are filled in
     ' (cannot have empty values)
     If String.IsNullOrEmpty(strUsername) Then _
          Throw New NullReferenceException(String.ConCat( _
               "The USERNAME ", _
               "variable for CreateNewUser() cannot be empty."))
     If String.IsNullOrEmpty(strFirstName) Then _
          Throw New NullReferenceException(String.ConCat( _
               "The FIRSTNAME ", _
               "variable for CreateNewUser() cannot be empty."))
     If String.IsNullOrEmpty(strLastName) Then _
          Throw New NullReferenceException(String.ConCat( _
               "The LASTNAME ", _
               "variable for CreateNewUser() cannot be empty."))
     If String.IsNullOrEmpty(strEmailAddress) Then _
          Throw New NullReferenceException(String.ConCat( _
               "The EMAILADDRESS ", _
               "variable for CreateNewUser() cannot be empty."))
     ' validate the e-mail address format
     ' I have a Regular Expression that is used globally for all e-mail
     ' validation... You can use your own here
     If Not _
     MYNAMESPACE.Common.Utilities.RegExLibrary.ValidateEmailFormat( _
          strEmailAddress _
          ) Then _
          Throw New Exception( _
          "The e-mail address was in an invalid format")
     '===========================================================
     '
     ' END VALIDATION
     '
     '===========================================================
     ' create a new user instance and populate it
     Me.User = New DotNetNuke.Entities.Users.UserInfo
     With Me.User
          .Profile.InitialiseProfile(Me.PortalSettings.PortalId)
          .AffiliateID = -1
          .DisplayName = String.Concat(strFirstName, " ", _
               strLastName)
          .Email = strEmailAddress
          .FirstName = strFirstName
          .IsSuperUser = False
          .Membership.Approved = blnMembershipApproved
          .Membership.CreatedDate = DateTime.Now
          .Membership.Email = strEmailAddress
          ' Use the built-in password generation, or your own
          ' Also, you can ammend this method to pass the password
          ' along with the other info
          '.Membership.Password = _
          '    DotNetNuke.Entities.Users.UserController.GeneratePassword
          .Membership.Password = _
               MYNAMESPACE.Security.PasswordGenerator.Generate(8, 10)
          ' Since the password is generated, force the visitor to
          ' change it when they log in
          .Membership.UpdatePassword = True
          .Membership.Username = strUsername
          .PortalID = Me.PortalSettings.PortalId
          .Username = strUsername
          .Profile.FirstName = strFirstName
          .Profile.LastName = strLastName
          .Profile.PreferredLocale = Me.PortalSettings.DefaultLanguage
          .Profile.TimeZone = Me.PortalSettings.TimeZoneOffset
     End With
     ' I am now handling this in the customized 
     ' profile/membership provider(s)...
     ' It is up to you as to how you want to handle this...
     ' but below should be your "default"
     ' Set the Approved status based on the Portal Settings
     'If Me.PortalSettings.UserRegistration = _
     '    Globals.PortalRegistrationType.PublicRegistration Then
     '    Me.User.Membership.Approved = True
     'Else
     '    Me.User.Membership.Approved = False
     'End If
     ' This is the line that actually performs the creation of the user
     ' (through the providers)
     ' attempt to create the DNN user
     Dim objStatus As UserCreateStatus = _
          DotNetNuke.Entities.Users.UserController.CreateUser(Me.User)
     ' set-up the arguments for the status of the user creation
     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
     ' I copied the DNN source code for notifying the user that the 
     ' user account was created, and it is referenced here...
     ' perform some DNN logic
     Me.UserCreateCompleted(args)
     ' return the enumerated result to be handled accordingly
     Return objStatus
End Function 
 
New Post
2/19/2009 3:50 PM
 

I'm implementing this for the first time in 4.9.0, and am consistently getting "UsernameAlreadyExists" as a UserCreateStatus.  I'm certain the user does *not* exist.  None of the tables (Users, aspnet_Users, etc) are being populated.

Did you encounter this as you wrote it?  I've copied your code pretty much verbatim.  Will attempt to post it separately if I can get it to format correctly in this editor.

Thanks!

 
New Post
2/19/2009 3:51 PM
 

    Public Sub CreateNewUser(ByVal strUsername As String, ByVal strFirstName As String, ByVal strLastName As String, _
        ByVal strEmailAddress As String)

        ' preliminary check to be sure that the fields are filled in     ' (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.")

        ' validate the e-mail address format    
        'If Not BBCO.Common.Utilities.RegExLibrary.ValidateEmailformat(strEmailAddress) Then _
        'Throw New Exception("The e-mail address was in an invalid format")

        ' create a new user instance and populate it    
        Dim objNewUser As UserInfo = New DotNetNuke.Entities.Users.UserInfo

        Response.Write("Portal ID: " & PortalId & "<br/>")

        With objNewUser
            .Profile.InitialiseProfile(PortalId)
            .DisplayName = String.Concat(strFirstName, " ", strLastName)
            .Email = strEmailAddress
            .FirstName = strFirstName
            .IsSuperUser = False
            .Membership.Approved = False
            .Membership.CreatedDate = DateTime.Now
            .Membership.Email = strEmailAddress
            .Membership.Password = DotNetNuke.Entities.Users.UserController.GeneratePassword
            .Membership.UpdatePassword = True
            .Membership.Username = strUsername
            .Username = strUsername
            .PortalID = PortalId
            .Profile.FirstName = strFirstName
            .Profile.LastName = strLastName
            .Profile.PreferredLocale = Me.PortalSettings.DefaultLanguage
            .Profile.TimeZone = Me.PortalSettings.TimeZoneOffset
        End With

        Response.Write("Username: " & objUser.Username & "<br/>")

        ' attempt to create the DNN user    
        Dim objStatus As UserCreateStatus = DotNetNuke.Entities.Users.UserController.CreateUser(objUser)
        Dim args As DotNetNuke.Entities.Modules.UserUserControlBase.UserCreatedEventArgs


        If objStatus = UserCreateStatus.Success Then
            args = New DotNetNuke.Entities.Modules.UserUserControlBase.UserCreatedEventArgs(objUser)
            args.Notify = True
        Else
            ' registration error         
            args = New DotNetNuke.Entities.Modules.UserUserControlBase.UserCreatedEventArgs(Nothing)
            args.Notify = False
        End If

        Response.Write("User Create Status: " & objStatus.ToString & " (" & strUsername & ")")

        args.CreateStatus = objStatus

    End Sub

 
New Post
2/19/2009 4:19 PM
 

GOT IT - my call to CreateUser was using another object (objUser, not objNewUser), this is the currently logged user, who obviously already exists.  Thanks for this code, it's great!!

 
New Post
2/19/2009 11:08 PM
 

Just now saw the forum reply...  Don't you love it when you answer your own question?  ;)  I do it all of the time.

 
Previous
 
Next
HomeHomeGeneralGeneralHow To, Tips & ...How To, Tips & ...HOW TO: Programatically Create A New User in DotNetNuke®HOW TO: Programatically Create A New User in DotNetNuke®


 
     
 
Forum Policy
 
 

These Forums are dedicated to the discussion of the Orlando DotNetNukeUG Users Group surrounding the kindred usage of DotNetNuke Web Application Framework.

For the benefit of the community and to protect the integrity of the UG, 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 DotNetNuke.
2. Discussion or promotion of DotNetNuke product releases under a different brand name are strictly prohibited.
3. No Flaming or Trolling.
4. No Profanity, Racism, or Prejudice.
5. Site Moderators have the final word on approving/removing a thread or post or comment.
6. English language posting only, please.

 
     
Terms Of Use | Privacy Statement
© 2008-2011 Orlando DotNetNuke® Users Group (ODUG)
DotNetNuke® Powered! v5.6.3.45
DotNetNuke® Powered! v05.01.00
Skin designed by Will Strohl
Website hosted by Applied Innovations
Orlando DotNetNuke® Users Group
DotNetNuke® is a registered trademark of the DotNetNuke® Corporation