This topic has not yet been rated - Rate this topic

PrincipalSearcher Class

Encapsulates the methods and search patterns used to execute a query against the underlying principal store.

System.Object
  System.DirectoryServices.AccountManagement.PrincipalSearcher

Namespace:  System.DirectoryServices.AccountManagement
Assembly:  System.DirectoryServices.AccountManagement (in System.DirectoryServices.AccountManagement.dll)
[DirectoryServicesPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
[DirectoryServicesPermissionAttribute(SecurityAction.InheritanceDemand, Unrestricted = true)]
public class PrincipalSearcher : IDisposable

The PrincipalSearcher type exposes the following members.

  Name Description
Public method PrincipalSearcher() Initializes a new instance of the PrincipalSearcher class. The QueryFilter property must be set before the Principal searcher object can be used to perform a search.
Public method PrincipalSearcher(Principal) Initializes a new instance of the PrincipalSearcher class with the specified query filter.
Top
  Name Description
Public property Context Gets that principal context that is used to perform the query. The context specifies the server or domain against which search operations are performed.
Public property QueryFilter Gets or sets the query filter that is used to locate matching principals.
Top
  Name Description
Public method Dispose Disposes the current instance of the PrincipalSearcher object.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method FindAll Returns a principal search result that contains a collection of all the principal objects that match the principal specified in the query filter property.
Public method FindOne Returns a principal search result that contains the first principal object found that matches the principal specified in the QueryFilter property.
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method GetUnderlyingSearcher Returns the underlying search object that is used by the Account Management API to perform the search.
Public method GetUnderlyingSearcherType Returns the type of the object returned from the GetUnderlyingSearcher method.
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Top

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.

.NET Framework

Supported in: 4, 3.5

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
VB.Net translation of the C# example
Imports System
Imports System.DirectoryServices.AccountManagement
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ctx As PrincipalContext = New PrincipalContext(ContextType.Machine)
        Dim up As UserPrincipal = New UserPrincipal(ctx)
        Dim ps As PrincipalSearcher = New PrincipalSearcher(up)
        Dim results As PrincipalSearchResult(Of Principal) = ps.FindAll
        For Each cr As Principal In results
            'Debug.Print(cr.Name)
            ListBox1.Items.Add(cr.Name)
        Next
        Me.Show()
    End Sub
End Class

Hope it helps.
Joe P.
Example: using PrincipalSearcher Class to get Computer List
//Get a list of computer names in MyDomain
PrincipalContext ctx = new PrincipalContext(ContextType.Domain,"MyDomain","DC=Mydomain,DC=Com");
System.Collections.ArrayList ComputerNames = new System.Collections.ArrayList();
ComputerPrincipal c = new ComputerPrincipal(ctx);
PrincipalSearcher ps = new PrincipalSearcher(c);
PrincipalSearchResult<Principal> results = ps.FindAll();
foreach (ComputerPrincipal cr in results)
{    
     ComputerNames.Add(cr.Name.ToString());
}


//regards, David KB5YLG