Solved

Active Directory PropertiesToLoad

Posted on 2004-04-22
6
1,424 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 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

12 Experts available now in Live!

Get 1:1 Help Now