Link to home
Start Free TrialLog in
Avatar of sej69
sej69

asked on

Missing properties in userPrincipal vb.net for user creation

I'm moving over a .vbs script to a vb.net compiled applciation.  I got the following code to work ok:
            pcContext = New PrincipalContext(ContextType.Domain, "mine.local", "ou=2022,dc=mine,dc=local", "administrator", "mypassword!!") 
            upUser = New UserPrincipal(pcContext)
            upUser.SamAccountName = szSAMAcctName
            upUser.GivenName = szFirst
            upUser.Surname = szLast
            upUser.MiddleName = szMiddle
            upUser.Description = szDescription
            upUser.HomeDirectory = szHomeDirectory
            upUser.DisplayName = szDisplayName
            upUser.EmployeeId = szID
            'upUser.
            upUser.Enabled = True
            'upUser.postalCode = szID
            upUser.Save()

Open in new window


And it creates the new user perfectly.  However, I need to use other fields in AD that show in active directory users and computers like office.  In VBSCript the field is called physicalDeliveryOfficeName but that field is not listed nor is there anything close to it.  The other field I want to use is postalCode.

How can I update these fields when I create the user?
Avatar of kjetilroe
kjetilroe
Flag of Norway image


I am assuming you are using .Net Framework 3.5 or newer, there you can use the ExtensionGet funktion to get any custom fields from the UserPrincipal object.

upUser.ExtensionGet("physicalDeliveryOfficeName")
upUser.ExtensionGet("postalCode")

You might have to check for null on the returnvalue in case no attribute exist for this name.

If not, here is a link to somebody who have done something similar in .Net Framework 2.0, that might help you on the right track:

http://www.vbforums.com/showthread.php?t=403284
Avatar of sej69
sej69

ASKER

I'm using .net 4.0.  How do I write to those fields?

My advice is that you set a breakpoint after creating the object, and explore your fields and fieldnames through the debugger.
ASKER CERTIFIED SOLUTION
Avatar of sej69
sej69

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Yes, C# is very similar to C++, so it is not a very big jump to go to C# instead of C++, except for all the extra functionality you get by going to C#.