Does anybody know how to query all groups with provided user in managed by field?
Thx in advance.

Can you please elaborate your problem?

We, cant get correctly what's ur problem.

I have a textbox field...I provide name in it (for example Lenon, John)
Function have to return all gruups with Lenon, John in "Managed by" field.

I've written code to search all groups with managedby field provided by user

String querymanagedby(string userName)
        {
            try
            {
                SearchResult result;
                DirectorySearcher search = new DirectorySearcher();
                search.Filter = String.Format("managedby={0})", userName);
                search.PropertiesToLoad.Add("cn");
                result = search.FindOne();

                StringBuilder userList = new StringBuilder();
                if (result != null)
                {
                    for (int counter = 0; counter <
                             result.Properties["managedby"].Count; counter++)
                    {
                        string group = (string)result.Properties["managedby"][counter];
                        string property = getproperty("LDAP://" + group, "samAccountName");
                        userList.Append(property);
                        userList.Append("\r\n");
                    }
                }
                return userList.ToString();
            }
            catch (Exception)
            {
                return null;
            }
        }

Unfortunately it doesn't work - can you help me?
THX!

In the code you have provided, exactly at which line of code you are getting Problem, can you tell me.

It does not return what i want (groups with managedby)

Program should work like that:

Please provide name: Lenon, John

and now program should list:

GROUP 1 is managed by Lenon, John
GROUP 2 is managed by Lenon, John
GROUP 3 is managed by Lenon, John

String querymanagedby(string userName)
        {
            try
            {
                string connectionPrefix = "LDAP://" + domain;
                DirectoryEntry entry = new DirectoryEntry(connectionPrefix);
                DirectorySearcher search = new DirectorySearcher(entry);
                search.Filter = String.Format("managedby={0})", userName);
                search.PropertiesToLoad.Add("samAccountName");
                StringBuilder userList = new StringBuilder();
                SearchResult result;
                SearchResultCollection resultcol = search.FindAll();
                if (resultcol != null)
                {
                    for (int counter = 0; counter < resultcol.Count; counter++)
                    {
                        result = resultcol[counter];
                        userList.Append((String)result.Properties["samAccountName"][0]);
                        userList.Append("\r\n");
                    }
                }
                return userList.ToString();
            }
            catch (Exception)
            {
                return null;
            }
        }

I have modified the code and it still return nothing. :(
Any ideas?

In your code, you have some errors at this line of code

search.Filter = String.Format[B]("managedby={0})"[/B], userName);

Correct it to

search.Filter = String.Format[B]("managedby={0}")[/B], userName);

and try once. See only the Bolded part of the code.

In your code, you have some errors at this line of code

search.Filter = String.Format[B]("managedby={0})"[/B], userName);

Correct it to

search.Filter = String.Format[B]("managedby={0}")[/B], userName);

and try once. See only the Bolded part of the code.

Sorry but you must be kidding. The Error he corrected is unfortunately an error but the correction is worse.

before:

search.Filter = String.Format[B]("managedby={0})"[/B], userName);

after:

search.Filter = String.Format("managedby=[B]([/B]{0})", userName);

See the difference? With the correction he provided you String.Format won't work.


I guess the Problem is youre search string. If you know that everything else around in your code is correct (because you copied it somewhere) and you change the filter...

final:

deSearch.Filter = String.Format("(managedby={0})", userName);

In your code, you have some errors at this line of code

search.Filter = String.Format[B]("managedby={0})"[/B], userName);

Correct it to

search.Filter = String.Format[B]("managedby={0}")[/B], userName);

and try once. See only the Bolded part of the code.

corrected code doesnt give any results too :(
new modification of my code:

String querymanagedby(string userName)
        {
            try
            {
                string connectionPrefix = "LDAP://" + domain;
                DirectoryEntry entry = new DirectoryEntry(connectionPrefix);
                DirectorySearcher search = new DirectorySearcher(entry);
                search.Filter = "(&(objectClass=group)(|(managedby={0}" + userName + ")))";
                search.PropertiesToLoad.Add("samAccountName");
               
                StringBuilder userList = new StringBuilder();
                SearchResult result;
                SearchResultCollection resultcol = search.FindAll();
                if (resultcol != null)
                {
                    for (int counter = 0; counter < resultcol.Count; counter++)
                    {
                        result = resultcol[counter];
                        userList.Append((String)result.Properties["samAccountName"][0]);
                        userList.Append("\r\n");
                    }
                }
                return userList.ToString();
            }
            catch (Exception)
            {
                return null;
            }
        }

Now he thinks a lot - so probably it search for sth..but still no results

First of all I have nearly the same code in one of my programms looking for users in a group. But before accessing them I have to cast to DirectoryEntry.

DirectoryEntry deUser = new DirectoryEntry(ADPath + "/" + pcoll["member"][l].ToString())

Also please speak with your local Active Directory administrator if you are allowed to read the field by code. A colleage of mine just walked inside and told me about a field that he was not allowed to read from.

I have all admin rights to query all things i want...
And about the line You have provided...
So You think I should make another directory entry for every result found in this query?...

I had the same problem and I just figured it out. I thought you might want to know.

For the filter, I was putting just the username instead of the full distinguished name. As soon as I changed it to be this filter it worked fine.

For example here is my filter (&(objectCategory=group)(managedby=CN=mckaaa,OU=INFO,OU......))

If you look under Active Directory, Attribute editor tab, you will find it's not a string but a distinguished name.

Andrea

Ok - I will try to put Whole DN as a input to the function. :)

It works... :D
The problem was {0} after managedby...
and ofcourse function needed full DN as a input

THX guys!!!!!!!!!!!!!!

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.