Aaron
Aaron

Reputation: 1

VB.NET DirectorySearcher Won't retrieve AD users with empty properties

I am using this code to retrieve a user from AD to a datagridview on a windows form. It works well so long as none of the properties are empty.

Private Sub Button9_Click(sender As System.Object, e As System.EventArgs) Handles Button9.Click   
 Dim dirEntry As System.DirectoryServices.DirectoryEntry
        Dim mySearcher As System.DirectoryServices.DirectorySearcher
        Dim domainName As String = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName



        Try
            dirEntry = New System.DirectoryServices.DirectoryEntry("LDAP://" & Environment.UserDomainName)

            mySearcher = New System.DirectoryServices.DirectorySearcher(dirEntry)
            mySearcher.Filter = "(samAccountName=" & TextBox8.Text & ")"

            mySearcher.PropertiesToLoad.Add("samAccountName")
            mySearcher.PropertiesToLoad.Add("DisplayName")
            mySearcher.PropertiesToLoad.Add("Description")
            mySearcher.PropertiesToLoad.Add("mail")
            mySearcher.PropertiesToLoad.Add("ProfilePath")
            mySearcher.PropertiesToLoad.Add("HomeDirectory")
            mySearcher.PropertiesToLoad.Add("HomeDrive")
            mySearcher.PropertiesToLoad.Add("GivenName")
            mySearcher.PropertiesToLoad.Add("sn")

            Dim sr As SearchResult = mySearcher.FindOne()
            If sr Is Nothing Then 'return false if user isn't found 
                'MsgBox("cannot find")
            End If

            Dim de As System.DirectoryServices.DirectoryEntry = sr.GetDirectoryEntry()

            Dim SAM As String = de.Properties("samAccountName").Item(0).ToString
            If SAM = "" Then
                SAM = " - "
            End If

            Dim DisplayName As String = de.Properties("DisplayName").Item(0).ToString
            If DisplayName = "" Then
                DisplayName = " - "
            End If

            Dim Description As String = de.Properties("Description").Item(0).ToString
            If Description = "" Then
                Description = " - "
            End If

            Dim Email As String = de.Properties("mail").Item(0).ToString
            If Email = "" Then
                Email = " - "
            End If

            Dim Profile As String = de.Properties("ProfilePath").Item(0).ToString
            If Profile = "" Then
                Profile = " - "
            End If

            Dim HomeDir As String = de.Properties("HomeDirectory").Item(0).ToString
            If HomeDir = "" Then
                HomeDir = " - "
            End If

            Dim HomeDrv As String = de.Properties("HomeDrive").Item(0).ToString
            If HomeDrv = "" Then
                HomeDrv = " - "
            End If

            Dim GivenName As String = de.Properties("GivenName").Item(0).ToString
            If GivenName = "" Then
                GivenName = " - "
            End If

            Dim Sn As String = de.Properties("sn").Item(0).ToString
            If Sn = "" Then
                Sn = " - "
            End If

            'DataGridView3.Rows.Add(de.Properties("samAccountName").Value.ToString, de.Properties("GivenName").Value.ToString & " " & de.Properties("sn").Value.ToString, de.Properties("ProfilePath").Value.ToString, de.Properties("HomeDirectory").Value.ToString, de.Properties("HomeDrive").Value.ToString, "", "", "", "", "")
            DataGridView3.AllowUserToAddRows = True
            DataGridView3.Rows.Add(SAM, DisplayName, Description, Email, Profile, "", HomeDir, "", HomeDrv, "")
            DataGridView3.AllowUserToAddRows = False

        Catch

        End Try
        dirEntry.Dispose()
        mySearcher.Dispose()



    End Sub

The parts of the code that use:

If SAM = "" Then
        SAM = " - "
    End If

were an attempt to combat this but doesnt work. If I find the user in AD and fill in the blank fields the user is retrieved.

Please can someone help me to identify the cause and how I might be able to resolve it.

For information...

I am using Visual Studio 2010

.Net 3.5 +

Many Thanks,

Aaron

Upvotes: 0

Views: 3268

Answers (1)

baldpate
baldpate

Reputation: 1749

  1. As I know on AD samAccountName cannot be null or empty.
  2. When an attribute is not set it will be null, but not "".
    For safety you may check both "" and null.

Upvotes: 0

Related Questions