2

I have an AD query, and adding properties manually, but I want the ability to add ALL active directory properties that a user can have to the searcher.

This is the current way i'm doing it, which works fine and dandy...

Dim de As New DirectoryEntry
If getset.impersonationset = True Then
    If getset.specificcontainerchecked = True Then
        de.Path = "LDAP://" & getset.containerstring()
        de.Username = getset.usernameset
        de.Password = getset.passwordset
    Else
        de.Path = "LDAP://" & getset.DomainName()
        de.Username = getset.usernameset
        de.Password = getset.passwordset
    End If
Else
    If getset.specificcontainerchecked = True Then
        de.Path = "LDAP://" & getset.containerstring()
    Else
        de.Path = "LDAP://" & getset.DomainName()
    End If
End If

Dim deSearch As New DirectorySearcher()
deSearch.SearchRoot = de
deSearch.Filter = "(&(objectClass=User)(objectCategory=Person))"
deSearch.PageSize = 1000
deSearch.SizeLimit = 1000

If getset.specificcontainerchecked = True Then
    If getset.subcontainers = True Then
        deSearch.SearchScope = SearchScope.Subtree
    ElseIf getset.subcontainers = False Then
        deSearch.SearchScope = SearchScope.OneLevel
    End If
ElseIf getset.specificcontainerchecked = False Then
    deSearch.SearchScope = SearchScope.Subtree
End If

deSearch.PropertiesToLoad.Add("sAMAccountName") 'Account Name
    deSearch.PropertiesToLoad.Add("givenName") 'Display Name
    deSearch.PropertiesToLoad.Add("sn") 'Load Users first name
    deSearch.PropertiesToLoad.Add("description") 'Description
    deSearch.PropertiesToLoad.Add("userAccountControl")   'Distinguished Name
    deSearch.PropertiesToLoad.Add("lastLogonTimestamp") 'Last Login
    deSearch.PropertiesToLoad.Add("whenCreated") 'Created Date
    deSearch.PropertiesToLoad.Add("whenChanged") 'Changed Date
    deSearch.PropertiesToLoad.Add("distinguishedName")
    deSearch.PropertiesToLoad.Add("msNPAllowDialin")
    deSearch.PropertiesToLoad.Add("cn") 'Wiles, Anthony
    deSearch.PropertiesToLoad.Add("co") 'United States
    deSearch.PropertiesToLoad.Add("company") 'Company
    deSearch.PropertiesToLoad.Add("l") 'Alpharetta
    deSearch.PropertiesToLoad.Add("mail") 'Email
    deSearch.PropertiesToLoad.Add("st") 'State

So I thought I would try to add them all, so a user could pick and choose which attributes they wanted... so I came up with this.

 Dim currSchema As ActiveDirectorySchema = ActiveDirectorySchema.GetCurrentSchema()
 Dim collection As ActiveDirectorySchemaClass = currSchema.FindClass("user")
 Dim properties As ReadOnlyActiveDirectorySchemaPropertyCollection = collection.GetAllProperties()
 Dim enumerator As IEnumerator = properties.GetEnumerator()
        While enumerator.MoveNext()
            Try
                deSearch.PropertiesToLoad.Add(enumerator.Current)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End While

But i'm getting the following error for most of them..

Conversion from type 'ActiveDirectorySchemaProperty' to type 'string' is not valid.

Any clues on what i'm missing? I realize it cannot cast ADSP to type string, but i'm not sure how to fix it. I'm sure some of them are Boolean, ints, datetime.

2 Answers 2

1

vb.net is not too strict to catch this in intellisense or even build. c# will catch this in intellisense.

change this line:

deSearch.PropertiesToLoad.Add(enumerator.Current)

to

deSearch.PropertiesToLoad.Add(enumerator.Current.ToString())

--------- C# version ------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using System.DirectoryServices.ActiveDirectory;
using System.Collections;

namespace AD
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectorySearcher deSearch = new DirectorySearcher();

        ActiveDirectorySchema currSchema = ActiveDirectorySchema.GetCurrentSchema();
        ActiveDirectorySchemaClass collection = currSchema.FindClass("user");
        ReadOnlyActiveDirectorySchemaPropertyCollection properties = collection.GetAllProperties();
        IEnumerator enumerator = properties.GetEnumerator();
        while (enumerator.MoveNext())
        {
            try
            {
                deSearch.PropertiesToLoad.Add(enumerator.Current.ToString());
                Console.WriteLine(enumerator.Current.ToString());
            }
            catch (Exception ex)
            {
               // MessageBox.Show(ex.Message);
            }
        }
    }
}

}

Sign up to request clarification or add additional context in comments.

4 Comments

Same error, though I have found it if I don't load anything it automatically pulls all of it. So that kind of answers my own question.
For what its worth it works fine for me ... tested it on both VB.Net and C# using VS2015.
When I catch exceptions, 40% of them fail, you look like you're ignoring them.
Just checked and ran my code again ... I don't get any exceptions. It never gets into the Catch Block of the C# code I posted earlier. Unless this has something to do with how my AD is setup at my Company as compared to yours.
1

The error is obvious and the other answer was correct. The enumerator.Current returns an object of ActiveDirectorySchemaProperty and not the string name that is required for PropertiesToLoad.Add(). As properly mentioned you need to use ToString

deSearch.PropertiesToLoad.Add(enumerator.Current.ToString)

Another way is to use collection returned by GetAllProperties() method (no need to add an additional enumerator if collection is already there):

For Each p As ActiveDirectorySchemaProperty In properties
   deSearch.PropertiesToLoad.Add(p.Name)
Next

To check if the code works you could check the Count property at the end

MessageBox.Show(deSearch.PropertiesToLoad.Count)

The only question is why do you need that? The PropertiesToLoad() is used to return properties that you wish to retrieve for the search result. For example, if you have a search page where user can search by certain criteria and search result will be displayed as a table with basic details such as username, display name, email etc. but not all. It makes no sense to load and return all attributes if there is no special need for that (and also because of performance reason). For example, the scheme of my "test" directory has over 800 attributes.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.