I am creating a new user in AD using UserPrincipal. Everything works as long as I enter a valid value.
I need to test for a string value from a textbox. In this case MiddleName. If there is a value in the textbox use that value, if the texbox value is emtpy, not insert a value or insert a null value.
Here is what I have so far:
Using context = New PrincipalContext(ContextType.Domain, "TUSTIN_NT")
Dim oUserPrincipal As New UserPrincipal(context, UserName, Password, True)
oUserPrincipal.UserPrincipalName = UserName + "@tusd.local"
oUserPrincipal.GivenName = FirstName
oUserPrincipal.MiddleName = MiddleName
If MiddleName IsNot Nothing Then
oUserPrincipal.MiddleName = MiddleName
Else
oUserPrincipal.MiddleName = MiddleName.Remove(0)
End If
oUserPrincipal.Surname = LastName
oUserPrincipal.DisplayName = LastName + ", " + FirstName
oUserPrincipal.EmailAddress = UserName + "@tustin.k12.ca.us"
oUserPrincipal.PasswordNeverExpires = True
oUserPrincipal.UserCannotChangePassword = True
oUserPrincipal.ScriptPath = ScriptPath
oUserPrincipal.Save()
Allan Browning
Vision Quest Integrated Technologies, Inc.
www.VisionQuestIT.com
If you solve your issue, please post the solution.
abrowning
Member
39 Points
68 Posts
Active Directory UserPrincipal empty string or null value
Oct 18, 2011 10:43 PM|LINK
I am creating a new user in AD using UserPrincipal. Everything works as long as I enter a valid value.
I need to test for a string value from a textbox. In this case MiddleName. If there is a value in the textbox use that value, if the texbox value is emtpy, not insert a value or insert a null value.
Here is what I have so far:
Using context = New PrincipalContext(ContextType.Domain, "TUSTIN_NT") Dim oUserPrincipal As New UserPrincipal(context, UserName, Password, True) oUserPrincipal.UserPrincipalName = UserName + "@tusd.local" oUserPrincipal.GivenName = FirstName oUserPrincipal.MiddleName = MiddleName If MiddleName IsNot Nothing Then oUserPrincipal.MiddleName = MiddleName Else oUserPrincipal.MiddleName = MiddleName.Remove(0) End If oUserPrincipal.Surname = LastName oUserPrincipal.DisplayName = LastName + ", " + FirstName oUserPrincipal.EmailAddress = UserName + "@tustin.k12.ca.us" oUserPrincipal.PasswordNeverExpires = True oUserPrincipal.UserCannotChangePassword = True oUserPrincipal.ScriptPath = ScriptPath oUserPrincipal.Save()Vision Quest Integrated Technologies, Inc.
www.VisionQuestIT.com
If you solve your issue, please post the solution.
kushal.dwive...
Member
396 Points
61 Posts
Re: Active Directory UserPrincipal empty string or null value
Oct 20, 2011 03:18 AM|LINK
If the value for MiddleName is empty, just don't set it to the UserPrincipal.MiddleName property.
try the following code :
Using context = New PrincipalContext(ContextType.Domain, "TUSTIN_NT")
Dim oUserPrincipal As New UserPrincipal(context, UserName, Password, True)
oUserPrincipal.UserPrincipalName = UserName + "@tusd.local"
oUserPrincipal.GivenName = FirstName
oUserPrincipal.MiddleName = MiddleName
If MiddleName IsNot Nothing Then
oUserPrincipal.MiddleName = MiddleName
End If
oUserPrincipal.Surname = LastName
oUserPrincipal.DisplayName = LastName + ", " + FirstName
oUserPrincipal.EmailAddress = UserName + "@tustin.k12.ca.us"
oUserPrincipal.PasswordNeverExpires = True
oUserPrincipal.UserCannotChangePassword = True
oUserPrincipal.ScriptPath = ScriptPath
oUserPrincipal.Save()