Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
DotNet
acad.assignment.mngr
academic
adonet
aspnet
aspnet.announcements
aspnet.build.controls
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
clr
compactframework
component_services
datatools
distributed_apps
drawing
faqs
framework
framework.wmi
general
internationalization
interop
languages.csharp
languages.jscript
languages.vb
languages.vb.controls
languages.vb.data
languages.vb.upgrade
languages.vc
languages.vc.libraries
myservices
odbcnet
performance
remoting
scripting
sdk
security
setup
vjsharp
vsa
webservi.enhancements
webservices
windowsforms
windowsforms.controls
winforms.databinding
winforms.designtime
xml
  
 
date: Wed, 7 Jun 2006 12:42:02 -0700,    group: microsoft.public.dotnet.distributed_apps        back       


Displaying information from an LDAP query w/VB.Net   
I've been asked to provide a simple query screen that displays information 
like name, department, phone number, etc. for our users.  I'm to pull this 
info from our Active Directory.  I'm using .Net 2.0 with VB.  Thanks to Mark 
Scheuner, I've got the query working, and I'm trying to output the givenName, 
displayName, department, distinguishedName, mail and telephoneNumber.  But no 
matter what I do, what displays for me is something in this format:

LDAP://CN=<distinguishedName>,CN=Users,DC=<domainName>,DC=com

What am I doing wrong?  Thanks!

Here's my code (I put asterisks in place of <domainname> in the strLDAPPath 
assignment):

<%@ Page Language="VB" Debug="true" CodeFile="Default.aspx.vb" 
Inherits="_Default" %>
<%@ Import Namespace="System.DirectoryServices" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">  
    <title>Untitled Page</title>
<script runat="server">
    Sub Page_Load(ByVal Source As Object, ByVal E As EventArgs)
        
        'Connect
        Dim strLDAPPath As String = ""
        strLDAPPath = "LDAP://dc=******,dc=com"
        Dim objSearchRoot As New DirectoryEntry(strLDAPPath)
        Dim objDirectorySearcher As New DirectorySearcher(objSearchRoot)
        
        'If search has been requested
        If IsPostBack = True Then
            
            'Restrict scope of search
            objDirectorySearcher.Filter = GetFilterString()
            objDirectorySearcher.PropertiesToLoad.Add("displayName")
            objDirectorySearcher.PropertiesToLoad.Add("department")
            objDirectorySearcher.PropertiesToLoad.Add("distinguishedName")
            objDirectorySearcher.PropertiesToLoad.Add("givenName")
            objDirectorySearcher.PropertiesToLoad.Add("mail")
            objDirectorySearcher.PropertiesToLoad.Add("telephoneNumber")
            objDirectorySearcher.PropertyNamesOnly = True
            objDirectorySearcher.CacheResults = True
            objDirectorySearcher.Sort = New SortOption("displayName", 
DirectoryServices.SortDirection.Ascending)
            'Get info from search box and search
            Dim objSearchResultsCollection As SearchResultCollection
            objSearchResultsCollection = objDirectorySearcher.FindAll()
            If Not (objSearchResultsCollection Is Nothing) Then
                'Get the Results
                Dim x As String
                x = ""
                For Each objSearchResult As SearchResult In 
objSearchResultsCollection
                    For Each strKey As String In 
objSearchResult.Properties.PropertyNames
                        Dim objValueCollection As 
ResultPropertyValueCollection
                        objValueCollection = 
objSearchResult.Properties(strKey)
                        Dim objPropertyValue As Object
                        For Each objPropertyValue In objValueCollection
                            x = x & " " & objPropertyValue.ToString
                        Next
                    Next strKey
                Next objSearchResult
                Message.InnerHtml = x
            Else
                Message.InnerHtml = "No hits"
            End If
        End If
        'Refresh object cache on local computer and clear variables
        objDirectorySearcher.CacheResults = False
        objDirectorySearcher.Dispose()
    End Sub
    
    'Format Query
    Public Function FormFilter(ByVal strCategory As String, ByVal strFName 
As String, ByVal strLName As String) As String
        Dim strResult As String
        strResult = "(&(objectCategory=" & strCategory & ")(displayName=" & 
strLName & "*)(givenName=" & strFName & "*))"
        Return strResult
    End Function
    
    'Get query string
    Public Function GetFilterString()
        Dim strFilter As String
        strFilter = ""
        strFilter = FormFilter("user", TextBox1.Text, TextBox2.Text)
        Return strFilter
    End Function
    
    </script>
</head>
<body>
    <form id="form1" runat="server" > 
        Search<br />First Name: <asp:TextBox ID="TextBox1" 
runat="server"></asp:TextBox>
        Last Name: <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Search" />
    </form>
    <div id="Message" runat="server" />
</body>
</html>
date: Wed, 7 Jun 2006 12:42:02 -0700   author:   Melanie Peterson

RE: Displaying information from an LDAP query w/VB.Net   
In case anyone stumbles across this, I still don't know why my original code 
doesn't work, but I found another way around it, plus I've linked my LDAP 
query results to a DataGrid.  Here's the code:

<%@ Page Language="VB" Debug="true" CodeFile="Default.aspx.vb" 
Inherits="_Default" %>
<%@ Import Namespace="System.DirectoryServices" %>
<%@ Import Namespace ="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">  
    <title>Untitled Page</title>
<script runat="server">
                    
    Sub Page_Load(ByVal Source As Object, ByVal E As EventArgs)
        
        'Connect
        Dim strLDAPPath As String = ""
        strLDAPPath = "LDAP://dc=<domainname goes here>,dc=com"
        Dim objSearchRoot As New DirectoryEntry(strLDAPPath)
        Dim objDirectorySearcher As New DirectorySearcher(objSearchRoot)
        
        'If search has been requested
        If IsPostBack = True Then
            
            'Restrict scope of search
            objDirectorySearcher.PropertiesToLoad.Add("cn")
            objDirectorySearcher.PropertiesToLoad.Add("displayName")
            objDirectorySearcher.PropertiesToLoad.Add("department")
            objDirectorySearcher.PropertiesToLoad.Add("givenName")
            objDirectorySearcher.PropertiesToLoad.Add("mail")
            objDirectorySearcher.PropertiesToLoad.Add("telephoneNumber")
            objDirectorySearcher.PropertiesToLoad.Add("description")
            
objDirectorySearcher.PropertiesToLoad.Add("physicalDeliveryOfficeName")
            
objDirectorySearcher.PropertiesToLoad.Add("facsimileTelephoneNumber")
            objDirectorySearcher.PropertiesToLoad.Add("objectClass")
            objDirectorySearcher.PropertyNamesOnly = True
            objDirectorySearcher.CacheResults = True
            objDirectorySearcher.Filter = GetFilterString()
            objDirectorySearcher.Sort = New SortOption("displayName", 
DirectoryServices.SortDirection.Ascending)
            
            'Get info from search box and search
            Dim objSearchResultsCollection As SearchResultCollection
            objSearchResultsCollection = objDirectorySearcher.FindAll()
            If objSearchResultsCollection.Count > 0 Then
                Dim objDataSet As New DataSet("ADDataSet")
                Dim objGridTable As New DataTable
                objGridTable = objDataSet.Tables.Add("objGridTable")
                Dim objGridRow As DataRow
                Dim objDataGrid As New DataGrid
                With objGridTable
                    .Columns.Add("ID", Type.GetType("System.String"))
                    .Columns.Add("Name", Type.GetType("System.String"))
                    .Columns.Add("Title", Type.GetType("System.String"))
                    .Columns.Add("Dept", Type.GetType("System.String"))
                    .Columns.Add("Office", Type.GetType("System.String"))
                    .Columns.Add("Email", Type.GetType("System.String"))
                    .Columns.Add("Phone", Type.GetType("System.String"))
                    .Columns.Add("Fax", Type.GetType("System.String"))
                End With

                'Get the Results                                        
                For Each objSearchResult As SearchResult In 
objDirectorySearcher.FindAll()
                    objGridRow = objGridTable.NewRow()
                    objGridRow("ID") = 
objSearchResult.GetDirectoryEntry().Properties("cn").Value.ToString
                    If (objSearchResult.Properties.Contains("displayName")) 
Then
                        objGridRow("Name") = 
objSearchResult.GetDirectoryEntry().Properties("displayName").Value.ToString
                    Else
                        objGridRow("Name") = "---"
                    End If
                    If (objSearchResult.Properties.Contains("description")) 
Then
                        objGridRow("Title") = 
objSearchResult.GetDirectoryEntry().Properties("description").Value.ToString
                    Else
                        objGridRow("Title") = "---"
                    End If
                    If (objSearchResult.Properties.Contains("department")) 
Then
                        objGridRow("Dept") = 
objSearchResult.GetDirectoryEntry().Properties("department").Value.ToString
                    Else
                        objGridRow("Dept") = "---"
                    End If
                    If 
(objSearchResult.Properties.Contains("physicalDeliveryOfficeName")) Then
                        objGridRow("Office") = 
objSearchResult.GetDirectoryEntry().Properties("physicalDeliveryOfficeName").Value.ToString
                    Else
                        objGridRow("Office") = "---"
                    End If
                    If (objSearchResult.Properties.Contains("mail")) Then
                        objGridRow("Email") = "<a href='mailto:" & 
objSearchResult.GetDirectoryEntry().Properties("mail").Value.ToString & _
                        "'>" & 
objSearchResult.GetDirectoryEntry().Properties("mail").Value.ToString & "</a>"
                    Else
                        objGridRow("Email") = "---"
                    End If
                    If 
(objSearchResult.Properties.Contains("telephoneNumber")) Then
                        objGridRow("Phone") = 
objSearchResult.GetDirectoryEntry().Properties("telephoneNumber").Value.ToString
                    Else
                        objGridRow("Phone") = "---"
                    End If
                    If 
(objSearchResult.Properties.Contains("facsimileTelephoneNumber")) Then
                        objGridRow("Fax") = 
objSearchResult.GetDirectoryEntry().Properties("facsimileTelephoneNumber").Value.ToString
                    Else
                        objGridRow("Fax") = "---"
                    End If
                    objGridTable.Rows.Add(objGridRow)

                Next objSearchResult
                
                ctrlResults.DataSource = objGridTable
                ctrlResults.DataBind()
                
            End If
            
        End If
        
        'Refresh object cache on local computer and clear variables
        objDirectorySearcher.CacheResults = False
        objDirectorySearcher.Dispose()
        
    End Sub
    
    'Format Query
    Public Function FormFilter(ByVal strCategory As String, _
    ByVal strFName As String, ByVal strLName As String, _
    ByVal strDept As String, ByVal strPhone As String) As String
        Dim strResult As String
        strResult = "(&(objectCategory=" & strCategory & _
        ")(displayName=" & strLName & "*)(givenName=" & strFName & _
        "*)(department=" & strDept & "*)(telephoneNumber=" & _
        strPhone & "*)(objectClass=user))"
        Return strResult
    End Function
    
    'Get query string
    Public Function GetFilterString()
        Dim strFilter As String
        strFilter = ""
        strFilter = FormFilter("user", TextBox1.Text, TextBox2.Text, 
TextBox3.Text, TextBox4.Text) ', TextBox5.Text)
        Return strFilter
    End Function
       
    </script>
</head>
<body>
    <form id="form1" runat="server" > 
        First Name: <asp:TextBox ID="TextBox1" 
runat="server"></asp:TextBox><br />
        Last Name: <asp:TextBox ID="TextBox2" 
runat="server"></asp:TextBox><br />
        Department: <asp:TextBox ID="TextBox3" 
runat="server"></asp:TextBox><br />
        Telephone Number: <asp:TextBox ID="TextBox4" 
runat="server"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="Search" />
        <input type = "reset" id = "Reset1" value = "Reset" runat = "server" 
/>
    </form>
    <asp:DataGrid ID="ctrlResults" BorderWidth="1" CellPadding="2" 
      CellSpacing="0" runat="server" >
    <ItemStyle HorizontalAlign="left"  VerticalAlign="Top" Font-Names 
="Verdana" Font-Size="Small" />
    <HeaderStyle Font-Bold="true" BackColor="#BBCEEC" />
    <AlternatingItemStyle BackColor="Bisque" />
    </asp:DataGrid>
</body>
</html>


"Melanie Peterson" wrote:

> I've been asked to provide a simple query screen that displays information 
> like name, department, phone number, etc. for our users.  I'm to pull this 
> info from our Active Directory.  I'm using .Net 2.0 with VB.  Thanks to Mark 
> Scheuner, I've got the query working, and I'm trying to output the givenName, 
> displayName, department, distinguishedName, mail and telephoneNumber.  But no 
> matter what I do, what displays for me is something in this format:
> 
> LDAP://CN=<distinguishedName>,CN=Users,DC=<domainName>,DC=com
> 
> What am I doing wrong?  Thanks!
> 
> Here's my code (I put asterisks in place of <domainname> in the strLDAPPath 
> assignment):
> 
> <%@ Page Language="VB" Debug="true" CodeFile="Default.aspx.vb" 
> Inherits="_Default" %>
> <%@ Import Namespace="System.DirectoryServices" %>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> 
> <html xmlns="http://www.w3.org/1999/xhtml" >
> <head runat="server">  
>     <title>Untitled Page</title>
> <script runat="server">
>     Sub Page_Load(ByVal Source As Object, ByVal E As EventArgs)
>         
>         'Connect
>         Dim strLDAPPath As String = ""
>         strLDAPPath = "LDAP://dc=******,dc=com"
>         Dim objSearchRoot As New DirectoryEntry(strLDAPPath)
>         Dim objDirectorySearcher As New DirectorySearcher(objSearchRoot)
>         
>         'If search has been requested
>         If IsPostBack = True Then
>             
>             'Restrict scope of search
>             objDirectorySearcher.Filter = GetFilterString()
>             objDirectorySearcher.PropertiesToLoad.Add("displayName")
>             objDirectorySearcher.PropertiesToLoad.Add("department")
>             objDirectorySearcher.PropertiesToLoad.Add("distinguishedName")
>             objDirectorySearcher.PropertiesToLoad.Add("givenName")
>             objDirectorySearcher.PropertiesToLoad.Add("mail")
>             objDirectorySearcher.PropertiesToLoad.Add("telephoneNumber")
>             objDirectorySearcher.PropertyNamesOnly = True
>             objDirectorySearcher.CacheResults = True
>             objDirectorySearcher.Sort = New SortOption("displayName", 
> DirectoryServices.SortDirection.Ascending)
>             'Get info from search box and search
>             Dim objSearchResultsCollection As SearchResultCollection
>             objSearchResultsCollection = objDirectorySearcher.FindAll()
>             If Not (objSearchResultsCollection Is Nothing) Then
>                 'Get the Results
>                 Dim x As String
>                 x = ""
>                 For Each objSearchResult As SearchResult In 
> objSearchResultsCollection
>                     For Each strKey As String In 
> objSearchResult.Properties.PropertyNames
>                         Dim objValueCollection As 
> ResultPropertyValueCollection
>                         objValueCollection = 
> objSearchResult.Properties(strKey)
>                         Dim objPropertyValue As Object
>                         For Each objPropertyValue In objValueCollection
>                             x = x & " " & objPropertyValue.ToString
>                         Next
>                     Next strKey
>                 Next objSearchResult
>                 Message.InnerHtml = x
>             Else
>                 Message.InnerHtml = "No hits"
>             End If
>         End If
>         'Refresh object cache on local computer and clear variables
>         objDirectorySearcher.CacheResults = False
>         objDirectorySearcher.Dispose()
>     End Sub
>     
>     'Format Query
>     Public Function FormFilter(ByVal strCategory As String, ByVal strFName 
> As String, ByVal strLName As String) As String
>         Dim strResult As String
>         strResult = "(&(objectCategory=" & strCategory & ")(displayName=" & 
> strLName & "*)(givenName=" & strFName & "*))"
>         Return strResult
>     End Function
>     
>     'Get query string
>     Public Function GetFilterString()
>         Dim strFilter As String
>         strFilter = ""
>         strFilter = FormFilter("user", TextBox1.Text, TextBox2.Text)
>         Return strFilter
>     End Function
>     
>     </script>
> </head>
> <body>
>     <form id="form1" runat="server" > 
>         Search<br />First Name: <asp:TextBox ID="TextBox1" 
> runat="server"></asp:TextBox>
>         Last Name: <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
>         <asp:Button ID="Button1" runat="server" Text="Search" />
>     </form>
>     <div id="Message" runat="server" />
> </body>
> </html>
>
date: Fri, 9 Jun 2006 14:07:02 -0700   author:   Melanie Peterson

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us