Can not retrieve one particular group users in Active Directory using PrincipalContext

95 views Asked by At

Getting exception

system.directoryservices.accountmanagement.principaloperationexception : There is no such object on the server

Trying to get all members of a group. Getting exception with one particular group only, able to get members of other groups with same code.

I am trying with following code

 public void GetADusers(string groupName, string domainName, string AdUser, string ADPassword, Hashtable SamName, Hashtable DisplayName, Hashtable POR, Hashtable Mail)
{
    try
    {
        string powergroup = System.Configuration.ConfigurationManager.AppSettings["PowerGroup"];
        string retrieveGroup = System.Configuration.ConfigurationManager.AppSettings["RetrieveGroup"];

        PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domainName, AdUser, ADPassword);
        GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, groupName);

        if (grp != null)
        {
            foreach (Principal p in grp.GetMembers(false))
            {
                if (!SamName.Contains(p.SamAccountName))
                {
                    SamName.Add(p.SamAccountName, p.SamAccountName);
                    DisplayName.Add(p.SamAccountName, p.DisplayName);
                    Mail.Add(p.SamAccountName, p.UserPrincipalName);
                    POR.Add(p.SamAccountName, false);
                }

                if ((groupName == powergroup || groupName == retrieveGroup) && SamName.Contains(p.SamAccountName))
                    POR[p.SamAccountName] = true;
            }

            grp.Dispose();
            ctx.Dispose();
        }
    }
    catch (Exception ex)
    {
        log.Error("usersByFilter: ", ex);
        throw ex;
    }
}

The group contains 600 members, some members are cross domain users

0

There are 0 answers