locked
Help getting all "Outlook" Active Directory users RRS feed

  • Question

  • User2001833234 posted

    Could someone please help:  I am trying to figure out a routine to fetch all active directory users (but only the ones that would appear in Microsoft Outlook.  When I trying running the following two sets of code, I get all users including admin etc.  How can I restrict the returned list to the users that would normally appear in outlook.

    This is from some code that I found on the net.  It works great for getting users that begin with char's.  I found the code at http://www.codegain.com/articles/activedirtocry/miscellaneous/all-operations-on-active-directory-ad-using-c-sharp.aspx

     public List<ADUserDetail> GetUsersByFirstName(string fName)
     {
                //UserProfile user;
                List<ADUserDetail> userlist = new List<ADUserDetail>();
                string filter = "";
     
                _directoryEntry = null;
                DirectorySearcher directorySearch = new DirectorySearcher(SearchRoot);
                directorySearch.Asynchronous = true;
                directorySearch.CacheResults = true            
            filter = string.Format("(givenName={0}*", fName);
                filter = "(&(objectClass=user)(objectCategory=person)(givenName=" + fName + "*))";
     
                directorySearch.Filter = filter;
                directorySearch.ReferralChasing = ReferralChasingOption.All;
     
                SearchResultCollection userCollection = directorySearch.FindAll();
     
                foreach (SearchResult users in userCollection)
                {
                    DirectoryEntry userEntry = new DirectoryEntry(users.Path, LDAPUser,
                                                                 LDAPPassword);
                    ADUserDetail userInfo = ADUserDetail.GetUser(userEntry);
     
                    userlist.Add(userInfo);
                }
     
                directorySearch.Filter = "(&(objectClass=group)(SAMAccountName=" + fName
                                           + "*))";
                SearchResultCollection results = directorySearch.FindAll();
                if (results != null)
                {
                    foreach (SearchResult r in results)
                    {
                        DirectoryEntry deGroup = new DirectoryEntry(r.Path, LDAPUser,
                                                                   LDAPPassword);
                        ADUserDetail agroup = ADUserDetail.GetUser(deGroup);
                        userlist.Add(agroup);
                    }
     
                }
                return userlist;
            }

    Also: I was able to find some code that does get all Users from Active directory, but again it literally get's all users.  I want to filter the list to be equal to that of Outlook.

     public List<ADUserDetail> GetAllUsers()
            {
                List<ADUserDetail> userlist = new List<ADUserDetail>();

                System.DirectoryServices.AccountManagement.PrincipalContext AD =
                    new System.DirectoryServices.AccountManagement.PrincipalContext(System.DirectoryServices.AccountManagement.ContextType.Domain, "OurDomain.com");
                System.DirectoryServices.AccountManagement.UserPrincipal u = new System.DirectoryServices.AccountManagement.UserPrincipal(AD);
                System.DirectoryServices.AccountManagement.PrincipalSearcher search = new System.DirectoryServices.AccountManagement.PrincipalSearcher(u);

                foreach (System.DirectoryServices.AccountManagement.UserPrincipal result in search.FindAll())
                {
                    if (result.DisplayName != null)
                    {

                        string test = result.EmailAddress;
                    }
                }

                return userlist;
            }

    I am obviously very lost trying to figure this stuff out.

    thanks

    Dave.

    Tuesday, April 30, 2013 5:02 PM

Answers

  • User1508394307 posted

    Dave, 

    if search by first name is not required, you can get rid of (givenName=" + fName + "*) 

    filter = "(&(objectClass=user)(objectCategory=person)(mail=*))";

    Not sure about objectCategory=Person, you can keep it or also delete as per MSDN example:

    filter = "(&(objectClass=user)(mail=*))"

    Hope this helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 30, 2013 5:59 PM

All replies

  • User1508394307 posted

    You can try to search for only mail-enabled users. For this, simply add (mail=*) in the query

    filter = "(&(objectClass=user)(objectCategory=person)(givenName=" + fName + "*)(mail=*))";

    Example from MSDN: http://support.microsoft.com/kb/252650

    Tuesday, April 30, 2013 5:47 PM
  • User2001833234 posted

    smirnov:  Thanks for the reply!

    Question, Using the above type search filter, what if I wanted to return all mail-enabled users?

    Dave.

    Tuesday, April 30, 2013 5:51 PM
  • User1508394307 posted

    Dave, 

    if search by first name is not required, you can get rid of (givenName=" + fName + "*) 

    filter = "(&(objectClass=user)(objectCategory=person)(mail=*))";

    Not sure about objectCategory=Person, you can keep it or also delete as per MSDN example:

    filter = "(&(objectClass=user)(mail=*))"

    Hope this helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 30, 2013 5:59 PM