?
Solved

Active Directory PropertiesToLoad

Posted on 2004-04-22
6
Medium Priority
?
1,435 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 750 total points
ID: 10903737
Where does de.SchemaClassName come from?

Bob
0
Technology Partners: 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!

 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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 anti-spam), the admin…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

850 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