[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Active Directory PropertiesToLoad

Posted on 2004-04-22
6
Medium Priority
?
1,434 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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 …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

649 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