DirectoryEntry.Rename method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dev99
    New Member
    • Mar 2013
    • 12

    DirectoryEntry.Rename method

    Writing a .Net 4 C# console application to add/update Active Directory user objects.

    I am using the DirectoryEntry. Rename method to rename the cn property of user objects.

    If I understand the MS documentation for the Rename method - - If UsePropertyCach e is true (which is the default) then I need to call CommitChanges on the object to make the rename action permanent.

    However, When I execute the following code - the Rename action becomes permanent without a CommitChanges.

    Code:
    DirectoryEntry Uentry = result.GetDirectoryEntry();
    Uentry.Rename("cn=" + newValue);
    Any advice on this would be appreciated?
  • Luk3r
    Contributor
    • Jan 2014
    • 300

    #2
    Do you have code that is specifying what a "result" is? What I mean by that is, your DirectoryServic es.SearchResult must return a result before it can actually change anything.

    I'm not sure how similar C# and VB are, but here's a small example in VB to give you something to go by. I tested this and it worked fine:
    Code:
    Dim ADServer As String = "LDAP://DC1"
            Dim dEntry As New DirectoryServices.DirectoryEntry(ADServer)
            Dim Searcher As New DirectoryServices.DirectorySearcher(dEntry)
            Searcher.Filter = ("(cn=Test User)")
            Dim entryResult As DirectoryServices.SearchResult
    
            Try
                For Each entryResult In Searcher.FindAll()
                    entryResult.GetDirectoryEntry.Rename("cn=Test Test")
                Next
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try

    Comment

    • dev99
      New Member
      • Mar 2013
      • 12

      #3
      Thanks for the reply. Yes my C# code is similar. In your code above - you do not specify CommitChanges() after the Rename. Don't you need this to make the Rename move from an update to your property cache to be a permanent change in the Directory?

      Comment

      • Luk3r
        Contributor
        • Jan 2014
        • 300

        #4
        If you have access to create a user in Active Directory, I suggest you do so. I didn't use CommitChanges() and my Test User account got switched to "Test Test" perfectly. So, if you can, create a test account, try your code without Commit and/or with Commit. If using CommitChanges() makes a difference, then keep it. Otherwise, omit it :)

        Comment

        • dev99
          New Member
          • Mar 2013
          • 12

          #5
          Thanks. I did discover that the Rename is permanent without requiring a CommitChanges. But that is contrary to MicroSoft docs.

          The docs state that I must specifically set UsePropertyCach e="false" (default is "true") in order for the Rename to be permanent by itself without the CommitChanges.

          So I'm just trying to verify if I understand the docs correctly. I'm also wondering if a CommitChanges happens when the C# script ends. Maybe that is where the update happens...?

          Comment

          • Luk3r
            Contributor
            • Jan 2014
            • 300

            #6
            According to Microsoft Documentation, you are correct. So I retract my other statement and suggest that you CommitChanges after making a change. I thought it was only used when Adding or Removing, not updating. My apologies, and cheers!

            Comment

            Working...