Reputation: 786
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.
Upvotes: 2
Views: 416
Reputation: 6814
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.
Upvotes: 1
Reputation: 1783
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);
}
}
}
}
}
Upvotes: 1