Search code examples
c#active-directoryldapdirectorysearcher

How do I build a filter to search for rooms in Active Directory in a .NET application?


I want to get all rooms in our company from active directory and for this I am using the DirectoryEntry and the DirectorySearcher classes. I need a filter that gets the right data but I don't know how I build the filter to search for the rooms.

Here is the attribute I need to filter by:

msExchResourceMetaData = ResourceType:Room

Here is my code:

public static DataTable GetRooms(string domaincontroller) 
{
    DataTable list = new DataTable();

    string filter = "(&(objectClass=user)(objectCategory=user)(|(telephoneNumber=*)(mail=*)))";

    //....

    return list; 
}

I need only the filter. The rest I know :(


Solution

  • The filter you have there will give you every user with a telephone number or an email address.

    If you are looking for rooms with a phone number or email address, just add in (msExchResourceMetaData=ResourceType:Room):

    string filter = "(&(objectClass=user)(objectCategory=user)(msExchResourceMetaData=ResourceType:Room)(|(telephoneNumber=*)(mail=*)))";
    

    If you want all rooms regardless of whether they have a telephone number or email address (which I'm guessing you do), then drop the unnecessary conditions:

    string filter = "(&(objectClass=user)(objectCategory=user)(msExchResourceMetaData=ResourceType:Room))";
    

    Note: make sure you're wrapping everything you can in using blocks when working with Active Directory, or you'll wind up with memory leaks.