Hi All,

I need desperate help.

I have a group "MyGroupABC" in Active Directory (AD). In this group there are close to 100 list of users.

How can i write a logic to actually to get me the list of users with their particulars details such as Name, SN, GivenName, Telephone, Mail, etc?

I've been trying many days to figuring it out, but not success.

Below is my simple code.

DirectorySearcher ds = new DirectorySearcher("");
ds.SearchRoot = new DirectoryEntry("LDAP://myserver/CN=MyGroupABC,OU=MyProject,OU=DEVApplications,DC=myserver,DC=local");
ds.PropertyNamesOnly = true;
ds.PropertiesToLoad.Add("name");
ds.PropertiesToLoad.Add("sn");
ds.PropertiesToLoad.Add("givenName");
ds.PropertiesToLoad.Add("mail");
ds.PropertiesToLoad.Add("telephoneNumber");

SearchResultCollection src = ds.FindAll();

try 
{
   foreach (SearchResult sr in src)
   {
      string str1 = de.name.ToString();
      string str2 = de.sn.ToString();
      string str3 = de.givenName.ToString();
      string str4 = de.mail.ToString();
      string str5 = de.telephoneNumber.ToString();
   }
}
catch (Exception e)
{
   MessageBox.Show(e.Message);
}

the ds.FindAll() result get returns empty.

Is there any better way to do around this?

Dani AI

Generated

— two reliable ways to get the user details you want (Name, sn, givenName, mail, telephoneNumber):

  1. Use the AccountManagement API (simpler, higher-level)
using (var ctx = new PrincipalContext(ContextType.Domain, "myserver.local"))
{
    var group = GroupPrincipal.FindByIdentity(ctx, "MyGroupABC");
    if (group != null)
    {
        foreach (var p in group.GetMembers(true))       // true = recurse nested groups
        {
            var user = p as UserPrincipal;
            if (user == null) continue;
            var de = user.GetUnderlyingObject() as DirectoryEntry;
            string name  = de.Properties["displayName"].Value as string;
            string sn    = de.Properties["sn"].Value as string;
            string given = de.Properties["givenName"].Value as string;
            string mail  = de.Properties["mail"].Value as string;
            string tel   = de.Properties["telephoneNumber"].Value as string;
            // store/use values
        }
    }
}
  1. If you prefer DirectorySearcher: either (A) read the group's member attribute and bind each member DN, or (B) search users with a memberOf filter. Example filter: (&(objectCategory=person)(objectClass=user)(memberOf=CN=MyGroupABC,OU=...,DC=myserver,DC=local)). Make sure PropertyNamesOnly is false and add the properties you need to PropertiesToLoad.

Troubleshooting / gotchas:

  • If you call this from a web app, run queries under a service account or middle tier (don’t embed high-privilege creds in public pages), as alluded to.
  • Use searcher.PageSize = 1000 for large groups to avoid size limits.
  • name is the RDN; displayName is usually what users expect.
  • Escape special characters if you place a DN inside an LDAP filter.
  • If results are empty, confirm the LDAP path, that you set Filter (or used the correct constructor), and that the account has read permission to the attributes.

Either approach works—AccountManagement is easier to read and maintain; use DirectorySearcher when you need fine-grained control.

Hi,

It could be a permissions error, naturally enough your AD is well protected - i'd never recommend querying it directly from a web page you're better building a comm object or something to do it for you and then just pulling back the data from that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.