Solved

Get phone list from AD and print nicely using PDF or other method

Posted on 2006-11-18
11
247 Views
Last Modified: 2012-08-13
Basically speaking, I need to use VB.Net to read AD account information, extract it, and hand off to a nicely printed solution, like an access report, a pdf document or anything else that would look nice. For a bonus 1000 points, they would like to choose which fields show up on the report, but that's just a bonus. Any ideas would be greatly appreciated.

0
Comment
Question by:Montoya
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 13

Expert Comment

by:newyuppie
ID: 17973147
what is AD?
0
 
LVL 19

Author Comment

by:Montoya
ID: 17973547
Active Directory, which is where I'd have to get the users' mailbox information after Exchange 5.5

0
 
LVL 13

Expert Comment

by:newyuppie
ID: 17973651
i believe you would use the System.DirectoryServices to directly access functions that will let you retrieve that info.  here are a couple of links i googled up to get you started:

http://www.vbdotnetheaven.com/UploadFile/johncharles/ActiveDirectoryInVB11122005060642AM/ActiveDirectoryInVB.aspx?ArticleID=a775db4b-3909-4d36-86e2-917b6d693465
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdirectoryservices.asp

when you have the info, to make reports you basically have 2 options: built-in vb net reporting, or third party. vb2005 comes with limited crystal reports reporting. you can also buy tools like XtraReports from devexpress.com to make nice, user customizable reports.

hope this info helps you.
NY
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Author Comment

by:Montoya
ID: 17981838
I was hoping for more guided directions or code, please, if anyone can help with this.
0
 
LVL 13

Expert Comment

by:newyuppie
ID: 17982352
were you hoping we do the work for you too?

read this codeproject link it may suit you:
http://www.codeproject.com/dotnet/activedirquery.asp

i also googled up this: http://www.15seconds.com/Issue/060525.htm
0
 
LVL 19

Author Comment

by:Montoya
ID: 17997958
no, I wasnt hoping you would do the work for me. I was hoping for some guidance, as stated above, or at least the same kind of help I try to provide as a guru in another section in this site, or the kind of help I would expect as a paying customer of this site.

Thanks for the links.

0
 
LVL 13

Expert Comment

by:newyuppie
ID: 17998208
hey im sorry, for personal reasons i was in a very bad mood that day.
wish i could help you more with the issue but i dont know much myself. hope you can make something out from those links
NY
0
 
LVL 19

Author Comment

by:Montoya
ID: 18007509
No problem. We all have our days. :)


If someone else has some insight to this issue, I'd greatly appreciated.
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 18007624
Hi Iammontoya,

Here is a sample that achieves what you want (in principle). You could easily extend this, at present it returns a datatable of people from activedirectory matching part of the cn property.

Imports System.DirectoryServices

....... standard stuff, class etc

    Private Sub GetPhoneBook()
        Dim strMatch As String = "John"
        Dim dtAD As New DataTable
        dtAD.TableName = "Default"
        dtAD.Columns.Add("UserName")
        dtAD.Columns.Add("Email Address")
        dtAD.Columns.Add("Telephone")
        dtAD.Columns.Add("Mobile")
        dtAD.Columns.Add("Fax")
        dtAD.Columns.Add("Title")
        dtAD.Columns.Add("Description")
        dtAD.Columns.Add("Facility")
        dtAD.Columns.Add("Address")
        dtAD.Columns.Add("Town")
        dtAD.Columns.Add("PostCode")
        If Not (strMatch Is Nothing) Then
            GetAD("corp.mydomain.com", dtAD, strMatch) 'specify the ad domain to search here, if multiple domains, can call GetAD() succesive times to return data to the same datatable
            dtAD.DefaultView.Sort = "UserName"
'here we do something with the data, in this case bind it to a datagrid, anything else is up to you.
            grdPeople.DataSource = dtAD
            grdPeople.DataBind()
        End If
    End Sub

    Private Sub GetAD(ByVal Root As String, ByRef dtAD As DataTable, ByVal Match As String)
        Dim strLDAP As String = "LDAP://" & Root
        Dim obDirEntry As DirectoryEntry = New DirectoryEntry(strLDAP)
        Dim srch As DirectorySearcher = New DirectorySearcher(obDirEntry)
        Dim results As SearchResultCollection
        srch.Filter = "(cn=*" & Match & "*)"
        Try
            results = srch.FindAll()
        Catch ex As Exception
            Exit Sub
        End Try
        Dim rstl As SearchResult
        For Each rstl In results
            Dim colProps As System.DirectoryServices.ResultPropertyCollection = rstl.Properties
            Dim aryElements(10) As String
            aryElements(0) = GetElement("displayname", colProps)
            aryElements(1) = "<a href=""mailto:" & GetElement("mail", colProps) & """>" & GetElement("mail", colProps) & "</a>" ' this was used on an asp.net page so converts the email address to a clickable link, probably need to change that for vb.net
            aryElements(2) = GetElement("telephonenumber", colProps)
            aryElements(3) = GetElement("mobile", colProps)
            aryElements(4) = GetElement("facsimiletelephonenumber", colProps)
            aryElements(5) = GetElement("title", colProps)
            aryElements(6) = GetElement("description", colProps)
            aryElements(7) = GetElement("physicaldeliveryofficename", colProps)
            aryElements(8) = GetElement("streetaddress", colProps)
            aryElements(9) = GetElement("l", colProps)
            aryElements(10) = GetElement("postalcode", colProps)
            If GetElement("objectClass", colProps).IndexOf("computer") < 0 And aryElements(0).Length > 0 Then
                dtAD.Rows.Add(aryElements)
            End If
        Next
    End Sub

    Private Function GetElement(ByVal Key As String, ByRef Props As ResultPropertyCollection) As String
        Try
            If Props(Key) Is Nothing Then
                Return ""
            Else
                Dim aryProp() As String
                ReDim aryProp(Props.Item(Key).Count - 1)
                Props.Item(Key).CopyTo(aryProp, 0)
                Return Join(aryProp, vbCrLf)
            End If
        Catch ex As Exception
            Return ""
        End Try
    End Function

Tim Cottee
0
 
LVL 1

Expert Comment

by:wpgatrel
ID: 18009676
Here is a simple VBScript that will read ALL user accounts in Active Directory and place the following items into a CSV file.  It will place the CSV file in the same location of the script.  You can use Excel to manipulate it and print it the way you want.

First Name, Last Name, Network Login, E-Mail Address, Telephone Number

'Script Start
Const ADS_SCOPE_SUBTREE = 2
CONST ForReading = 1, ForWriting = 2, ForAppending = 8

Dim DomainName, ParentDir, Server, Lengthy, Lengthy2, Drive, szGivenName, szSN
Dim Command1, WSHNetwork, FS, Domain, DomainObj, Computer, ShareServiceObj, Hidden, DrivePath, nReturnCode

Set objFSO = CreateObject("scripting.FileSystemObject")

LogFile="ContactInfo.CSV"

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

strDNSDomain = objRootDSE.Get("DefaultNamingContext")

Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = "Select * from 'LDAP://" & strDNSDomain & "' Where objectClass='User'"  
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst

set f = objFSO.OpenTextFile(LogFile, ForWriting, True, -2)

f.writeline "First Name,Last Name,Network Login,Email Address,Telephone"

Do Until objRecordSet.EOF


      szSamAccountName = objRecordSet.Fields("samAccountName").Value
      szGivenName = objRecordSet.Fields("GivenName").Value
      szSN = objRecordSet.Fields("SN").Value
      szCN = "CN=" & szGivenName & " " & szSN

      f.writeline szGivenName & "," & szSN & "," & szSamAccountName & "," & Email & "," & telephone

      
objRecordSet.MoveNext
Loop

f.close

wscript.echo "Script Complete!"
'Script Complete
0
 
LVL 1

Accepted Solution

by:
wpgatrel earned 500 total points
ID: 18009698
Doh!  I meant to paste in THIS code.

'Script Start
Set objFSO = CreateObject("scripting.FileSystemObject")

LogFile="ContactInfo.CSV"

Set objRootDSE = GetObject("LDAP://RootDSE")
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

strDNSDomain = objRootDSE.Get("DefaultNamingContext")

Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = "Select samAccountName, GivenName, SN, telephonenumber, mail from 'LDAP://" & strDNSDomain & "' Where objectClass='User'"  
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst

set f = objFSO.OpenTextFile(LogFile, ForWriting, True, -2)

f.writeline "First Name,Last Name,Network Login,Email Address,Telephone"

Do Until objRecordSet.EOF

      szSamAccountName = objRecordSet.Fields("samAccountName").Value
      szGivenName = objRecordSet.Fields("GivenName").Value
      szSN = objRecordSet.Fields("SN").Value
      szMail = objRecordSet.Fields("Mail").Value
      szTelephoneNumber = objRecordSet.Fields("TelephoneNumber").value

      f.writeline szGivenName & "," & szSN & "," & szSamAccountName & "," & szMail & "," & sztelephoneNumber

      
objRecordSet.MoveNext
Loop

f.close

wscript.echo "Script Complete!"
'Script End
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question