Small width layout Medium width layout Maximum width layout Small text Medium text Large text
     Search
Downloads Downloads Directory Directory Forums Forums Forge Forge Blogs Blogs        Marketplace Marketplace Careers Program Careers
Community › Forums Register  |  

PortalWebHosting
  Ads  
Iron Speed Designer is a software development tool for building database, reporting, and forms applications for .NET without hand-coding.
 


  Sponsors  

Meet Our Sponsors

Salaro -- Skins and more
OnyakTech
CrystalTech Web Hosting™
Webhost4life, specialists in DNN hosting
Mad Development is a full service interactive agency focusing on the merge of design, technology, e-commerce, and affiliate marketing by providing total website solutions.
SteadyRain
 


DotNetNuke Forums
 
  Forum  General DotNetN...  Extend It! ( Pr...  HOW TO: Programmatically Create New User
Previous Previous
 
Next Next
New Post 5/29/2007 3:13 PM
User is offline Will Strohl
1374 posts
www.WillStrohl.com
6th Ranked




HOW TO: Programmatically Create New User 

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 LinkedIn profile
DNN Training Videos | My Blog | DotNetNuke® Skins by SSD
Google Toolbar Button for DNN | Suggested DNN Upgrade Paths
ODUG Web Site
 
New Post 5/30/2007 9:43 AM
User is offline Fooberichu
467 posts
www.seeleyware.com
8th Ranked


Re: HOW TO: Programmatically Create New User 

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");


-- Fooberichu
http://www.seeleyware.com
 
New Post 5/30/2007 9:57 AM
User is offline Will Strohl
1374 posts
www.WillStrohl.com
6th Ranked




Re: HOW TO: Programmatically Create New User 

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 LinkedIn profile
DNN Training Videos | My Blog | DotNetNuke® Skins by SSD
Google Toolbar Button for DNN | Suggested DNN Upgrade Paths
ODUG Web Site
 
New Post 5/31/2007 2:50 PM
User is offline Will Strohl
1374 posts
www.WillStrohl.com
6th Ranked




Re: HOW TO: Programmatically Create New User 
Modified By Will Strohl  on 6/5/2007 3:58:22 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 LinkedIn profile
DNN Training Videos | My Blog | DotNetNuke® Skins by SSD
Google Toolbar Button for DNN | Suggested DNN Upgrade Paths
ODUG Web Site
 
New Post 6/5/2007 2:21 PM
User is offline Fooberichu
467 posts
www.seeleyware.com
8th Ranked


Re: HOW TO: Programmatically Create New User 

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.


-- Fooberichu
http://www.seeleyware.com
 
Previous Previous
 
Next Next
  Forum  General DotNetN...  Extend It! ( Pr...  HOW TO: Programmatically Create New User
 


Forum Policy

These Discussion Forums are dedicated to the discussion of the DotNetNuke Web Application Framework.

For the benefit of the community and to protect the integrity of the project, 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.

 


PartnerPoint | Community of Microsoft Partners
PartnerPoint is one of the largest and most active online communities of Microsoft Partners worlwide with over 8,000 members.
www.PartnerPoint.com
Web Valley
Website design, Database development
www.webvalley.com
UK DotNetNuke CMS installation, hosting & support
UK based installation, branding, customising, integration, hosting, training, support and maintenance services for DotNetNuke
www.deburca.co.uk

DotNetNuke Corporation   Terms Of Use  Privacy Statement
DotNetNuke®, DNN®, and the DotNetNuke logo are trademarks of DotNetNuke Corporation
Hosted by MaximumASP