How to find exchange server users?

         DirectoryEntry entry = new DirectoryEntry(_ldap, userName, password); // _ldap is domain such as  "LDAP://xxx.xxx.com";
         DirectorySearcher ds = new DirectorySearcher(entry);

         ds.Filter = String.Format("(&(objectCategory=person)(objectClass=user)(|(displayname={0}*)(lastname={0}*)(mail={0}*)))", keyword); // search by these fields

         ds.PropertiesToLoad.AddRange(new string[] { "samaccountname", "displayname", "mail", "l" });

         ds.SizeLimit = 50; // result number returned

         SearchResultCollection results = ds.FindAll();

 foreach (SearchResult result in results)

{

ResultPropertyCollection resultProperties = result.Properties;

               if (resultProperties.Contains("mail"))
                  name = resultProperties["mail"][0].ToString();
   


  // Should use "Contains" check first to improve performance, otherwise if use resultProperties["mail"] directly, when resultProperties doesn't has field  "mail", performance is bad.

}

posted on 2012-11-12 16:54  chuwachen  阅读(88)  评论(0)    收藏  举报