1

I'm having a heck of a time trying to add fields like department and title.

I'm using this to create a user account:

Dim ctx As New PrincipalContext(ContextType.Domain, "domain.name.pvt",  "OU=Users,DC=global,DC=pvt")

Dim usr As UserPrincipal = New UserPrincipal(ctx) 

I have no problem creating the account but can't add simple things like Department and Title. I read about using extensions but its in C++ and have no clue on how to do it.

Any help would be great!!! Thanks!

1 Answer 1

1

If you're on .NET 3.5 and up, you should check out the System.DirectoryServices.AccountManagement (S.DS.AM) namespace. Read all about it here:

To extend the UserPrincipal class, you don't need much - something like this will suffice (I wrote this in C# originally and just converted it to VB.NET on the 'net - I hope there's no issues with the VB.NET code!)

Imports System.DirectoryServices.AccountManagement

Namespace ADExtended
    <DirectoryRdnPrefix("CN")> _
    <DirectoryObjectClass("Person")> _
    Public Class UserPrincipalEx
        Inherits UserPrincipal
        ' Inplement the constructor using the base class constructor. 
        Public Sub New(context As PrincipalContext)
            MyBase.New(context)
        End Sub

        ' Implement the constructor with initialization parameters.    
        Public Sub New(context As PrincipalContext, samAccountName As String, password As String, enabled As Boolean)
            MyBase.New(context, samAccountName, password, enabled)
        End Sub

        ' Create the "Department" property.    
        <DirectoryProperty("department")> _
        Public Property Department() As String
            Get
                If ExtensionGet("department").Length <> 1 Then
                    Return String.Empty
                End If

                Return DirectCast(ExtensionGet("department")(0), String)
            End Get
            Set
                ExtensionSet("department", value)
            End Set
        End Property

        ' Create the "Title" property.    
        <DirectoryProperty("title")> _
        Public Property Title() As String
            Get
                If ExtensionGet("title").Length <> 1 Then
                    Return String.Empty
                End If

                Return DirectCast(ExtensionGet("title")(0), String)
            End Get
            Set
                ExtensionSet("title", value)
            End Set
        End Property

        ' Implement the overloaded search method FindByIdentity.
        Public Shared Shadows Function FindByIdentity(context As PrincipalContext, identityValue As String) As UserPrincipalEx
            Return DirectCast(FindByIdentityWithType(context, GetType(UserPrincipalEx), identityValue), UserPrincipalEx)
        End Function

        ' Implement the overloaded search method FindByIdentity. 
        Public Shared Shadows Function FindByIdentity(context As PrincipalContext, identityType As IdentityType, identityValue As String) As UserPrincipalEx
            Return DirectCast(FindByIdentityWithType(context, GetType(UserPrincipalEx), identityType, identityValue), UserPrincipalEx)
        End Function
    End Class
End Namespace

Now, you just use the UserPrincipalEx class:

Dim ctx As New PrincipalContext(ContextType.Domain, "domain.name.pvt",  "OU=Users,DC=global,DC=pvt")

Dim usr As UserPrincipalEx = New UserPrincipalEx(ctx) 
usr.Title = "......."
usr.Department = "......."

The new S.DS.AM makes it really easy to play around with users and groups in AD!

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! I will try it. Im a novice to any of the namespacess. I will try to work with that thanks soo much!!
Thanks, i didnt get it to work :( boo :( When i entered in the code it said too many arguments in the Public Sub New(). thats the message when i hover over (ctx)
Have you tried creating a new user with the UserPrincipalEx class? I receive errors saying "The server was unwilling to process the request". Other functionality seems to work great like updating department or employeeID.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.