By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,382 Members | 1,659 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,382 IT Pros & Developers. It's quick & easy.

Active Directory LDAP Phone List

P: 3
Hi all,

Note that I'm new to LDAP and Active Directory and am writing an application that retrieves a simple phone list for all the users in our domain. So far I have the following console app code that seems to work. It queries Active Directory for a phone list and correctly retrieves the names (cn) from the OU. Though I have asked for properties like telephoneNumber, homeNumber, mobile, ipphone, facsimileTelephoneNumber...the properties are all populated by the land line i.e. telephoneNumber. e.g.

User: John Doe, Phone: 3256 2222, Home: 3256 2222, Cell: 3256 2222, IPPhone: 3256 2222, Fax: 3256 2222
User: Jane Doe, Phone: 5556 2222, Home: 5556 2222, Cell: 5556 2222, IPPhone: 5556 2222, Fax: 5556 2222


Ideally they should populate with the correct fax, mobile no e.g.

User: John Doe, Phone: 3256 2222, Home: 3256 1234, Cell: 0256 221356, IPPhone: n/a, Fax: 3256 2258
User: Jane Doe, Phone: 5556 2222, Home: 5556 1253, Cell: 0325 788356, IPPhone: 782145 , Fax: n/a
etc

I don't know where I'm going wrong and would appreciate any assistance. The code is as below:



Expand|Select|Wrap|Line Numbers
  1. Imports System.DirectoryServices
  2. Imports System.DirectoryServices.ActiveDirectory
  3. Imports System
  4. Imports System.Collections
  5. Imports System.Collections.Specialized
  6.  
  7.  
  8. Module Module1
  9.  
  10.     Sub Main()
  11.  
  12.         Dim LDAP As String = 
  13. "LDAP://OU=Perth,DC=cnn,DC=com,DC=au" (This is an example, use your own domain e.g."LDAP://CNN".
  14.  
  15.         Dim origWidth, width As Integer
  16.         Dim origHeight, height As Integer
  17.  
  18.         origWidth = Console.WindowWidth
  19.         origHeight = Console.WindowHeight
  20.         width = origWidth * 1.5
  21.         height = origHeight * 2.5
  22.  
  23.         Console.SetWindowSize(width, height)
  24.  
  25.         Dim entry As New DirectoryEntry(LDAP, Nothing, Nothing, DirectoryServices.AuthenticationTypes.Secure)
  26.  
  27.         Dim ADSearcher As New DirectorySearcher(entry)
  28.         ADSearcher.Filter = "(&(objectCategory=person)(objectClass=user))"
  29.  
  30.         '"(objectCategory=person)"
  31.  
  32.         With ADSearcher
  33.             .SearchRoot = entry
  34.             .PropertiesToLoad.Add("telephoneNumber")
  35.             .PropertiesToLoad.Add("homeNumber")
  36.             .PropertiesToLoad.Add("mobile")
  37.             .PropertiesToLoad.Add("ipphone")
  38.             .PropertiesToLoad.Add("facsimileTelephoneNumber")
  39.             .PropertiesToLoad.Add("cn")
  40.         End With
  41.  
  42.  
  43.         Dim adColl As System.DirectoryServices.SearchResultCollection
  44.         Dim Phone, Cell, Home, IPhone, Fax, CN As System.DirectoryServices.ResultPropertyValueCollec  tion
  45.  
  46.         Try
  47.  
  48.             adColl = ADSearcher.FindAll()
  49.  
  50.             Dim i As Integer
  51.  
  52.             For i = 0 To adColl.Count - 1
  53.                 Dim adEntry As SearchResult = adColl(i)
  54.                 Phone = adEntry.Properties("telephoneNumber")
  55.                 Home = adEntry.Properties("homeNumber")
  56.                 Cell = adEntry.Properties("mobile")
  57.                 iphone = adEntry.Properties("ipphone")
  58.                 Fax = adEntry.Properties("facsimileTelephoneNumber")
  59.                 CN = adEntry.Properties("cn")
  60.  
  61.                 Dim user As String = CN.Item(0)
  62.                 Dim phoneNumber As String = ""
  63.                 Dim homeNumber As String = ""
  64.                 Dim cellNumber As String = ""
  65.                 Dim ipphone As String = ""
  66.                 Dim Fx As String = ""
  67.  
  68.                 Try
  69.                     phoneNumber = Phone.Item(0).ToString
  70.                     homeNumber = Phone.Item(0).ToString
  71.                     cellNumber = Phone.Item(0).ToString
  72.                     ipphone = Phone.Item(0).ToString
  73.                     Fx = Phone.Item(0).ToString
  74.  
  75.                 Catch ex As Exception
  76.                     'No phone number exists.
  77.                     phoneNumber = "n/a"
  78.                     homeNumber = "n/a"
  79.                     cellNumber = "n/a"
  80.                     ipphone = "n/a"
  81.                     Fx = "n/a"
  82.                 End Try
  83.  
  84.                 Console.WriteLine(String.Format("User: {0}, Phone: {1}, Home: {2}, Cell: {3}, IPPhone: {4}, Fax: {5}", user, phoneNumber, homeNumber, cellNumber, ipphone, Fx))
  85.  
  86.             Next i
  87.             Console.ReadLine()
  88.  
  89.         Catch ex As Exception
  90.             Console.WriteLine(ex.Message.ToString)
  91.         Finally
  92.             adColl.Dispose()
  93.             ADSearcher.Dispose()
  94.             entry.Dispose()
  95.         End Try
  96.  
  97.     End Sub
  98.  
  99. End Module
____________

regards

Hems
Jul 5 '07 #1
Share this Question
Share on Google+
1 Reply


Expert 5K+
P: 8,434
I'm quite lost with this, have never even looked at LDAP. But doesn't this code...
phoneNumber = Phone.Item(0).ToString
homeNumber = Phone.Item(0).ToString
cellNumber = Phone.Item(0).ToString
ipphone = Phone.Item(0).ToString
Fx = Phone.Item(0).ToString
explicitly say to set them all to the same thing?
Jul 5 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.