Parent problem with Directory Entry

Last post 08-28-2003 7:28 PM by dunnry. 3 replies.

Sort Posts:

  • Parent problem with Directory Entry

    03-11-2003, 5:13 AM
    • Member
      5 point Member
    • turbodude
    • Member since 03-11-2003, 4:55 AM
    • Posts 1
    Hello,

    I need some help. I have had this big problem a couple of days and I don't know what to do. I want to get a new directory entry with the guid of a contact that exists in Active Direcotory. Then I want to get the parent (a organizational unit) and retrieve its properties.

    Here is the code:

    Imports System.DirectoryServices
    ....
    Dim sobjectGUID As String = "b967ce0b-019f-4595-98cf-717a3fe5fc0f"
    Dim oDe As New DirectoryEntry("LDAP://domain.com/<guid=" & sobjectGUID & ">", "username", "password")
    oDe = oDe.Parent

    When I connect to the active directory at the server (domain.com) from my local computer and run the code above I am able to retrieve the properties from the parent.

    Though when I copy the code to a project on the server (domain.com) I am just able to retrieve properties from the first directory entry (sobjectGUID) but when I try to set oDe to its parent (oDe = oDe.Parent) I get the top root object (domain.com).

    Anyone who has got an idea what to do?

    /turbo
  • Re: Parent problem with Directory Entry

    03-11-2003, 8:41 AM
    • Star
      9,098 point Star
    • dunnry
    • Member since 06-24-2002, 12:17 PM
    • http://directoryprogramming.net
    • Posts 1,806
    • TrustedFriends-MVPs
    Interesting... I am surprised you could access the parent at all in any server. Binding by guid has the disadvantage that you cannot determine the parent or children of an object because they are not loaded when initially bound.

    There is a work-around for this. You need to rebind to the object using it's distinguishedName, and then .Parent will work for you:

    Dim sobjectGUID As String = "b967ce0b-019f-4595-98cf-717a3fe5fc0f"
    Dim oDe As New DirectoryEntry("LDAP://domain.com/<guid=" & sobjectGUID & ">", "username", "password")
    oDe = new DirectoryEntry("LDAP://" & oDe.Properties("distinguishedName")(0).ToString(), "username", "password")
    oDe = oDe.Parent

    Something similar to this should work...

  • Re: Parent problem with Directory Entry

    08-28-2003, 6:24 PM
    • Member
      60 point Member
    • glo
    • Member since 10-16-2002, 4:35 PM
    • Posts 12
    How do you find out the parent path after you find the user using directorysearcher. The FindAll locate the user record but directorysearchercollection doesn't have a parent property....so how do I find out his parent path/OU name!!

    Here is my code:

    Dim adPath As String = "LDAP://OU=SSI,DC=ssicorp,DC=pvt"
    Dim adEntry As New DirectoryEntry(adPath)

    Dim adSearcher As New DirectorySearcher(adEntry)
    adSearcher.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" & LoginID & "))"

    adSearcher.PropertiesToLoad.Add("cn")
    adSearcher.PropertiesToLoad.Add("sAMAccountName")
    adSearcher.PropertiesToLoad.Add("mail")

    ' define a datatable and add the results to it
    Dim adResults As SearchResultCollection
    Dim dt As New DataTable("AD_Users")
    dt.Columns.Add(New DataColumn("AccName", GetType(System.String)))
    dt.Columns.Add(New DataColumn("Name", GetType(System.String)))
    dt.Columns.Add(New DataColumn("Email", GetType(System.String)))
    Dim dr As DataRow
    adResults = adSearcher.FindAll

    For Each adResult As SearchResult In adResults
    dr = dt.NewRow()
    dr(0) = adResult.Properties("sAMAccountName")(0).ToString()
    dr(1) = adResult.Properties("cn")(0).ToString()
    dr(2) = adResult.Properties("mail")(0).ToString()
    dt.Rows.Add(dr)
    Next

    Any helps would be greatly appreciated. Thanks.

    Gabriela Lo
  • Re: Parent problem with Directory Entry

    08-28-2003, 7:28 PM
    • Star
      9,098 point Star
    • dunnry
    • Member since 06-24-2002, 12:17 PM
    • http://directoryprogramming.net
    • Posts 1,806
    • TrustedFriends-MVPs
    You would need to do something like this with your SearchResult:
    Dim user as DirectoryEntry = adResult.GetDirectoryEntry()
    
    Dim parent as DirectoryEntry = user.Parent 'here is the parent
Page 1 of 1 (4 items)