PrincipalSearcher Class
Encapsulates the methods and search patterns used to execute a query against the underlying principal store.
Assembly: System.DirectoryServices.AccountManagement (in System.DirectoryServices.AccountManagement.dll)
The default page size of 256 KB is used when the PrincipalSearcher class performs a query for domain principals. The application may override the default value by setting the PageSize property in the underlying DirectorySearcher object that is returned from the GetUnderlyingSearcher method.
Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
The PrincipalSearcher & GroupPrincipal
using
System.Collections.Generic;using System.DirectoryServices.AccountManagement;
using System.Linq;
namespace MSDN.Example
{
publicclassAccountManagement
{
///<summary>
/// Searches the local domain for all group principals
///</summary>
///<returns>A list of group principals if successful; otherwise null</returns>
///<remarks>This method uses objects that are new in version 3 of the .Net Framework</remarks>
publicList<GroupPrincipal> GetAllGroups()
{
List<GroupPrincipal> retVal = null;
//Create a context for the search take place in
using (PrincipalContext context = newPrincipalContext(ContextType.Domain))
{
//Create a group principal filter with the context
using (GroupPrincipal filter = newGroupPrincipal(context))
{
//Create a principal search using the filter
using (PrincipalSearcher searcher = newPrincipalSearcher(filter))
{
//Execute a search with the searcher using the filter against the context of the local domain.
//searcher.FindAll is called in a LINQ expression to provide the collection of GroupPrincipals.
//The LINQ expression wrapped in parents to define scope and followed by a call to ToList which
//will convert the IEnumerable returned by the LINQ expression into a generic List<GroupPrincipal>
retVal = (from principal in searcher.FindAll() select principal asGroupPrincipal).ToList();
}
}
}
return retVal;
}
}
}
- 6/18/2008
- Derik Palacino
- 6/30/2008
- Thomas Lee