Solved

Active Directory PropertiesToLoad

Posted on 2004-04-22
6
1,427 Views
Last Modified: 2008-02-01
     
WIth this code below, I enter in a value in a textbox "Luck" and it will search "Luck*" in active directory and return any users that begin with those letters.  It will return the name and I use a strbuilder to concatinate the usernames without the full name (cn=Full Name, cn=Users, ...ect)  I will end up with a list:

Returned Query
   Lucky Inc
   Lucky Dog
   Lucky Duck

I want to display the propertiestoload("department") and ("telephonenumber") but I keep getting the error....Object not set to a reference....Does anyone know how I could add those items to the Query?  And how to write those values?


       lblNode.Text = de.Name.Substring(3)
        ' form the filter string for directory search
        Dim filter As String = ""
        filter = txtName.Text & "*"
        Dim objectCategory As String = "user"
        Dim result As String
        result = String.Format("(&(objectCategory={0})(name={1}))", objectCategory, filter)

        Dim ds As New DirectorySearcher
        ds.SearchRoot = New DirectoryEntry("") ' start searching from local domain
        ds.Filter = result ' get the LDAP filter string based on selections on the form
        ds.PropertyNamesOnly = True ' this will get names of only those properties to which a value is set
        ds.PropertiesToLoad.Add("name")
        ds.Sort = New SortOption("name", SortDirection.Ascending)

        Dim src As SearchResultCollection = ds.FindAll()
        Dim groupNames As StringBuilder = New System.Text.StringBuilder
        Dim equalsIndex, commaIndex As Int32

        Try
            Dim sr As SearchResult
            'Write the Count of Records
            Response.Write(src.Count & "<br>")
            For Each sr In src
                If String.Compare(de.SchemaClassName, "User", True) Then
                   
                    equalsIndex = sr.Path.IndexOf("=", 1)
                    commaIndex = sr.Path.IndexOf(",", 1)
                    groupNames.Append(sr.Path.Substring(equalsIndex + 1, commaIndex - equalsIndex - 1))
                    groupNames.Append("<br>")
                    'Response.Write(sr.Path)
                    'Response.Write("<br>")

                End If
            Next sr

            'Write Returned Search After Loop
            Response.Write("<br>" & groupNames.ToString & "<br>")
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
        src.Dispose()
        ds.Dispose()
0
Comment
Question by:luckyinc
  • 4
  • 2
6 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 10894888
What line are you getting the error on?

Bob
0
 

Author Comment

by:luckyinc
ID: 10899212


  ds.Filter = result ' get the LDAP filter string based on selections on the form
  ds.PropertyNamesOnly = True ' this will get names of only those properties to which a value is set
'REPLACED THIS LINE OF CODE FROM ABOVE  ************************************

 ds.PropertiesToLoad.AddRange(New String() {"cn", "sn", "mail", "givenname", "telephonenumber", "department"})

***********************************************************************************


            Dim sr As SearchResult
            'Write the Count of Records
            Response.Write(src.Count & "<br>")
            For Each sr In src
                If String.Compare(de.SchemaClassName, "User", True) Then


******ADDED THIS CODE TO SEE IF EACH PERSON HAD AN EMAIL ADDRESS
                    If sr.Properties.Contains("mail") Then
                        Dim emailAddress As String
   *******ERROR HERE - - CANNOT CONVERT resultvaluepropertycollection TO STRING
                        emailAddress = CStr(sr.Properties("mail"))
                        Response.Write(emailAddress)
                    End If

                    equalsIndex = sr.Path.IndexOf("=", 1)
                    commaIndex = sr.Path.IndexOf(",", 1)
                    groupNames.Append(sr.Path.Substring(equalsIndex + 1, commaIndex - equalsIndex - 1))
                    groupNames.Append("<br>")
                    'Response.Write(sr.Path)
                    'Response.Write("<br>")

                End If
            Next sr


If I remove the convert to string, and put   -   sr.Properties("mail").tostring  -  it prints a line for each person that has an email, but it is displaying the path:  System.DirectoryServices.ResultPropertyValueCollectionSystem
for as many times as the person DOES have an email address in active directory.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 250 total points
ID: 10903737
Where does de.SchemaClassName come from?

Bob
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:luckyinc
ID: 10918079
It is declaired as a Public variable
Dim de as new directory entry
0
 

Author Comment

by:luckyinc
ID: 10918080
It is declaired as a Public variable
Dim de as new directory entry
0
 

Author Comment

by:luckyinc
ID: 10921775
Dim entryUserPath As New DirectoryServices.DirectoryEntry(sr.Path)


I added that after my if statement....
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

832 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