How to update the CN field of an existing user

Last post 09-26-2007 11:29 AM by johram. 1 replies.

Sort Posts:

  • How to update the CN field of an existing user

    09-04-2007, 4:11 PM
    • Loading...
    • demmett
    • Joined on 09-04-2007, 8:00 PM
    • Posts 1

    I have the following code that allows me to update a user's information in Active Directory:

    Dim lcresult As String = ""
    Dim lcPath As String
    Dim sStep As String
    Dim sDisplayName As String
    Dim sUserPrincipalName As String

    sDisplayName = sFirstname + " " + sLastname
    sUserPrincipalName = sEmail.Replace(
    "@", "$") + "@" + lcServer

    Try

    Dim connectionPrefix As String = "LDAP://" + lcServer + "/" + lcDomain
    Dim entry As DirectoryEntry = New DirectoryEntry(connectionPrefix, lcADUser, lcADPassword)
    Dim mySearcher As DirectorySearcher = New DirectorySearcher(entry)

    mySearcher.Filter = "(&(objectCategory=user)(objectClass=user)(objectSID=" + ObjectSidSearch(sObjectSID) + "))"

    Dim result As SearchResult = mySearcher.FindOne()

    If result Is Nothing Then
    sMessage = "User was not found"
    Return -1
    End If

    lcPath = result.Path
    sStep =
    "After search and found user"

    Dim adUser As DirectoryEntry = New DirectoryEntry(lcPath, lcADUser, lcADPassword)

    'adUser.Properties("cn").Value = sDisplayName

    adUser.Properties("userPrincipalName").Value = sUserPrincipalName
    adUser.Properties(
    "givenName").Value = sFirstname
    adUser.Properties(
    "sn").Value = sLastname
    adUser.Properties(
    "displayName").Value = sDisplayName
    adUser.Properties(
    "mail").Value = sEmail
    adUser.CommitChanges()
    adUser.Close()
    adUser.Dispose()

    sStep = "After updating password"

    Catch ex As Exception

    sMessage = "ERROR: " + ex.ToString()

    Return -1

    End Try

    sMessage = sStep

    Return 0

    The code listed above works when I have the line 'adUser.Properties("cn").Value = sDisplayName commented out.
    When I uncomment the line, I get the following error: The directory service cannot perform the requested operation on the RDN attribute of an object.

    I now know this is not the correct way to update the CN field, but I have not been able to find any examples.

  • Re: How to update the CN field of an existing user

    09-26-2007, 11:29 AM
    • Loading...
    • johram
    • Joined on 06-13-2006, 10:36 AM
    • Sweden
    • Posts 3,352
    • Moderator

    To update the CN field (the actual name of the object in the AD), you need to use the Rename method of the DirectoryEntry.

    entry.Rename("newName")
     
     

     

    If this post was useful to you, please mark it as answer. Thank you!
Page 1 of 1 (2 items)
Microsoft Communities
Page view counter