Solved

Active Directory PropertiesToLoad

Posted on 2004-04-22
6
1,423 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
Comment Utility
What line are you getting the error on?

Bob
0
 

Author Comment

by:luckyinc
Comment Utility


  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
Comment Utility
Where does de.SchemaClassName come from?

Bob
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

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

Author Comment

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

Author Comment

by:luckyinc
Comment Utility
Dim entryUserPath As New DirectoryServices.DirectoryEntry(sr.Path)


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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now