471,319 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

Getting users from a specifc AD group

I want to get a list of users from a specific AD group. I use this
code:

************************************************** **************************************
SearchResult result;
DirectorySearcher search = new DirectorySearcher();
search.Filter = String.Format("(cn={0})", groupName);
search.PropertiesToLoad.Add("member");
result = search.FindOne();

ArrayList userNames = new ArrayList();
if (result != null)
{
for (int counter = 0; counter <
result.Properties["member"].Count; counter++)
{
string user = (string)result.Properties["member"][counter];
userNames.Add(user);
}
}

************************************************** **************************************
When the code is finished user string = "CN=Egholm Jacob (DFDS
A/S),OU=Development,OU=IT,OU=DFDS AS,DC=dk,DC=dfds,DC=root"

But what I want to do is getting information like givenName,
displayName etc. from the user.
What shall I do?

Jan 4 '06 #1
5 9510
You should load the givenName and displayName property from the AD.

string[] props = {"givenName ", "displayName", ".."};
search.PropertiesToLoad.AddRange(props);
....

Willy.

<eg****@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
I want to get a list of users from a specific AD group. I use this
code:

************************************************** **************************************
SearchResult result;
DirectorySearcher search = new DirectorySearcher();
search.Filter = String.Format("(cn={0})", groupName);
search.PropertiesToLoad.Add("member");
result = search.FindOne();

ArrayList userNames = new ArrayList();
if (result != null)
{
for (int counter = 0; counter <
result.Properties["member"].Count; counter++)
{
string user = (string)result.Properties["member"][counter];
userNames.Add(user);
}
}

************************************************** **************************************
When the code is finished user string = "CN=Egholm Jacob (DFDS
A/S),OU=Development,OU=IT,OU=DFDS AS,DC=dk,DC=dfds,DC=root"

But what I want to do is getting information like givenName,
displayName etc. from the user.
What shall I do?

Jan 4 '06 #2
Thanks for your reply. I have change my code to this:

************************************************** ***************************************

SearchResult result;
DirectorySearcher search = new DirectorySearcher();
search.Filter = String.Format("(cn={0})", groupName);
search.PropertiesToLoad.Add("member");
search.PropertiesToLoad.Add("displayName");
result = search.FindOne();
ArrayList userNames = new ArrayList();
if (result != null)
{
for (int counter = 0; counter <
result.Properties["member"].Count; counter++)
{
string user = (string)result.Properties["member"][counter];

string test =
(string)result.Properties["displayName"][counter];
userNames.Add(user);
}
}
************************************************** ***************************************
The string variable test returns the name of the group and not the name
of the user in the group.

Jan 4 '06 #3


<eg****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Thanks for your reply. I have change my code to this:

************************************************** ***************************************

SearchResult result;
DirectorySearcher search = new DirectorySearcher();
search.Filter = String.Format("(cn={0})", groupName);
search.PropertiesToLoad.Add("member");
search.PropertiesToLoad.Add("displayName");
result = search.FindOne();
ArrayList userNames = new ArrayList();
if (result != null)
{
for (int counter = 0; counter <
result.Properties["member"].Count; counter++)
{
string user = (string)result.Properties["member"][counter];

string test =
(string)result.Properties["displayName"][counter];
userNames.Add(user);
}
}
************************************************** ***************************************
The string variable test returns the name of the group and not the name
of the user in the group.

If your cn denotes a "username", you should apply the following filter.

src.Filter = "(objectCategory=user)";

also, you should not use a for loop to iterate, use foreach instead.
Willy.
Jan 4 '06 #4
Hello Willy

I have change my code (look below), but it still dosen't work.

************************************************** ***************************************
DirectoryEntry searchRoot = new DirectoryEntry(@"LDAP://dfds.root");
DirectorySearcher search = new DirectorySearcher(searchRoot);
search.Filter = "(&(cn=Group CFS Handlers)(objectCategory=user))";
search.PropertiesToLoad.Add("samaccountname");

foreach (SearchResult res in search.FindAll())
{
string dummy = res.Properties["samaccountname"].ToString();
}
************************************************** ***************************************

I want to list all the users in the group "Group CFS Handlers", but
search.FindAll() returns 0 results. I just checked the AD and the group
"Group CFS Handlers" contain one user.
I you have any idea what I do wrong, please let my know. I believe it
has something to do with my filter, but I don't know what.

Jan 5 '06 #5
Hello Willy

I have change my code (look below), but it still dosen't work.

************************************************** ***************************************
DirectoryEntry searchRoot = new DirectoryEntry(@"LDAP://dfds.root");
DirectorySearcher search = new DirectorySearcher(searchRoot);
search.Filter = "(&(cn=Group CFS Handlers)(objectCategory=user))";
search.PropertiesToLoad.Add("samaccountname");

foreach (SearchResult res in search.FindAll())
{
string dummy = res.Properties["samaccountname"].ToString();
}
************************************************** ***************************************

I want to list all the users in the group "Group CFS Handlers", but
search.FindAll() returns 0 results. I just checked the AD and the group
"Group CFS Handlers" contain one user.
I you have any idea what I do wrong, please let my know. I believe it
has something to do with my filter, but I don't know what.

Jan 5 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by BobTheDatabaseBoy | last post: by
5 posts views Thread by Mike Turco | last post: by
11 posts views Thread by Derek Martin | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.