Results 1 to 10 of 10

Thread: Solved] retrieve information from Active Directory

  1. #1

    Thread Starter
    Lively Member darktown's Avatar
    Join Date
    Apr 2006
    Posts
    85

    Solved] retrieve information from Active Directory

    Hi,

    I would like to write a vb-code to retrieve information from our active directory. How do i do this?

    I have tried 10 several codes, surfed the web and I just don't manage to do it.
    I always have problems with the "Imports System.DirectoryServices", is underlined in green & using

    directorysearcher or directoryentry does not work.
    Last edited by darktown; Jun 23rd, 2006 at 09:07 AM.
    There is an easy way, there is a hard way and there is My way of doing things!

  2. #2
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: [2005] retrieve information from Active Directory

    Can you post the code? And what is the exact error/warning message that you are getting?

    Take a look at this article
    http://channel9.msdn.com/Showpost.aspx?postid=132740

    Have you added the reference to System.DirectoryServices.DLL from Add References menu option?
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  3. #3

    Thread Starter
    Lively Member darktown's Avatar
    Join Date
    Apr 2006
    Posts
    85

    Re: [2005] retrieve information from Active Directory

    oh, thx for the info, i am trying some stuff now
    There is an easy way, there is a hard way and there is My way of doing things!

  4. #4

    Thread Starter
    Lively Member darktown's Avatar
    Join Date
    Apr 2006
    Posts
    85

    Re: [2005] retrieve information from Active Directory

    okey, this works:

    VB Code:
    1. Dim objSearch As New DirectorySearcher()
    2.         objSearch.SearchRoot = New DirectoryEntry("LDAP://verelst.nw", "jkhadmin", "Tidus05")
    3.         objSearch.Filter = "(&(objectclass=user)(objectcategory=person))"
    4.         objSearch.SearchScope = SearchScope.Subtree
    5.         objSearch.PropertiesToLoad.Add("cn")
    6.  
    7.         Dim colQueryResults As SearchResultCollection
    8.         colQueryResults = objSearch.FindAll()
    9.  
    10.         Dim objResult As SearchResult
    11.         For Each objResult In colQueryResults
    12.             Label1.Text += objResult.Properties("cn")(0) & vbCrLf
    13.         Next

    the question: where can i find a list of the attribute names so I can make a nice overview?
    There is an easy way, there is a hard way and there is My way of doing things!

  5. #5
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: [2005] retrieve information from Active Directory

    Quote Originally Posted by darktown
    the question: where can i find a list of the attribute names so I can make a nice overview?
    I did not understand what exactly dod you mean by attribute names.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  6. #6

    Thread Starter
    Lively Member darktown's Avatar
    Join Date
    Apr 2006
    Posts
    85

    Re: [2005] retrieve information from Active Directory

    VB Code:
    1. Dim objSearch As New DirectorySearcher()
    2.         objSearch.SearchRoot = New DirectoryEntry("LDAP://verelst.nw", "jkhadmin", "Tidus05")
    3.         objSearch.Filter = "(&(objectclass=user)(objectcategory=person))"
    4.         objSearch.SearchScope = SearchScope.Subtree
    5.         objSearch.PropertiesToLoad.Add("cn")
    6.         objSearch.PropertiesToLoad.AddRange(New String() {"cn", "sn", "test"})
    7.  
    8.         Dim colQueryResults As SearchResultCollection
    9.         colQueryResults = objSearch.FindAll()
    10.  
    11.         Dim objResult As SearchResult
    12.         For Each objResult In colQueryResults
    13.             Label1.Text += "<br/>" & objResult.Properties("cn")(0) & "|" & objResult.GetDirectoryEntry().Properties("sAMAccountName").Value.ToString()
    14.         Next

    I mean "saMAccountName", I have searched the web but can't seem to find the attributename for the "companyname, telephone , website and others"..

    When you look in the properties of a user in you AD, you"ll find these fields, i want to retrieve them & alter them.

    I have found a way to retrieve them now, am looking how to retrieve all the fields I want & then I want to search & edit the ones I want


    I have found this link: http://www.computerperformance.co.uk..._directory.htm

    But it doesn't work.
    Response.Write("|" & objResult.GetDirectoryEntry().Properties("mail").Value.ToString)

    Get a "object not set to a reference" error.. use the "new" keyword ..
    Last edited by darktown; Jun 22nd, 2006 at 09:16 AM.
    There is an easy way, there is a hard way and there is My way of doing things!

  7. #7

    Thread Starter
    Lively Member darktown's Avatar
    Join Date
    Apr 2006
    Posts
    85

    Re: [2005] retrieve information from Active Directory

    found a nice article and it works: http://forums.asp.net/thread/1309113.aspx
    But i also want to be able to edit the field, there is a link but it's all in C-sharp and i don't understand it.

    could someone explain & intergrate it in my code :

    Code:
    Dim filter As String = String.Format("(&(objectClass=user)(objectCategory=person)(sAMAccountName={0}))", Environment.UserName)
            Dim entry As New DirectoryEntry("LDAP://verelst.nw", "jkhadmin", "Tidus05")
            Dim searcher As New DirectorySearcher(entry, filter, New String() {"displayName", "streetAddress", "l", "st", "postalCode", "physicalDeliveryOfficeName", "mail"})
            Dim fullName As String = ""
    
            Try
    
                Dim result As SearchResult = searcher.FindOne()
    
                If Not IsNothing(result) Then
    
                    If (result.Properties.Contains("displayName")) Then
                        lblName.Text = "Name"
                        txtCity.Text = result.Properties("displayName")(0).ToString
                    End If
    
                    If (result.Properties.Contains("streetAddress")) Then
                        lblAddress.Text = result.Properties("streetAddress")(0)
                    End If
    
                    If (result.Properties.Contains("l")) Then
                        lblcity.Text = result.Properties("l")(0)
                    End If
    
                    If (result.Properties.Contains("st")) Then
                        lblstate.Text = result.Properties("st")(0)
                    End If
    
                    If (result.Properties.Contains("postalCode")) Then
                        lblzip.Text = result.Properties("postalCode")(0)
                    End If
    
                    If (result.Properties.Contains("physicalDeliveryOfficeName")) Then
                        lblphone.Text = result.Properties("physicalDeliveryOfficeName")(0)
                    End If
    
                    If (result.Properties.Contains("mail")) Then
                        lblDesc.Text = result.Properties("mail")(0)
                    End If
                End If
    
            Catch
    
                Throw
    
            Finally
    
                If Not (entry Is Nothing) Then
    
                    entry.Dispose()
    
                End If
    
                If Not (searcher Is Nothing) Then
                    searcher.Dispose()
                End If
            End Try
    There is an easy way, there is a hard way and there is My way of doing things!

  8. #8
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: [2005] retrieve information from Active Directory

    There are lot of converteres over there which can convert C# code in VB.NET and vice versa. Try one of those.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  9. #9

    Thread Starter
    Lively Member darktown's Avatar
    Join Date
    Apr 2006
    Posts
    85

    Re: [2005] retrieve information from Active Directory

    Don't bother found my solution
    There is an easy way, there is a hard way and there is My way of doing things!

  10. #10
    Lively Member
    Join Date
    Nov 2007
    Location
    Seattle, WA
    Posts
    76

    Thumbs up Re: Solved] retrieve information from Active Directory

    i like the way you have this coded. i am creating an app that uses the directorySearcher class and I had everything working great except if i wanted to view all the attributes for a large number of users the search would take FOREVER! this is the only code that i have seen that works nice and fast!!! this simple part is the code i am refering to...

    Code:
    result.Properties("displayName")(0).ToString
    everything else if have seen calls the result.getdirectoryentry property which then returns all properties even if you specify to only load a specific set. not to mention when you do this it is slow, slow, slow!!!! thanks man!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width