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.
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?
Re: [2005] retrieve information from Active Directory
oh, thx for the info, i am trying some stuff now
Re: [2005] retrieve information from Active Directory
okey, this works:
VB Code:
Dim objSearch As New DirectorySearcher()
objSearch.SearchRoot = New DirectoryEntry("LDAP://verelst.nw", "jkhadmin", "Tidus05")
objSearch.Filter = "(&(objectclass=user)(objectcategory=person))"
objSearch.SearchScope = SearchScope.Subtree
objSearch.PropertiesToLoad.Add("cn")
Dim colQueryResults As SearchResultCollection
colQueryResults = objSearch.FindAll()
Dim objResult As SearchResult
For Each objResult In colQueryResults
Label1.Text += objResult.Properties("cn")(0) & vbCrLf
Next
the question: where can i find a list of the attribute names so I can make a nice overview?
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.
Re: [2005] retrieve information from Active Directory
VB Code:
Dim objSearch As New DirectorySearcher()
objSearch.SearchRoot = New DirectoryEntry("LDAP://verelst.nw", "jkhadmin", "Tidus05")
objSearch.Filter = "(&(objectclass=user)(objectcategory=person))"
objSearch.SearchScope = SearchScope.Subtree
objSearch.PropertiesToLoad.Add("cn")
objSearch.PropertiesToLoad.AddRange(New String() {"cn", "sn", "test"})
Dim colQueryResults As SearchResultCollection
colQueryResults = objSearch.FindAll()
Dim objResult As SearchResult
For Each objResult In colQueryResults
Label1.Text += "<br/>" & objResult.Properties("cn")(0) & "|" & objResult.GetDirectoryEntry().Properties("sAMAccountName").Value.ToString()
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 ..
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
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.
Re: [2005] retrieve information from Active Directory
Don't bother found my solution
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!!!:bigyello: :thumb: :bigyello: 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!