We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Query Active Directory groups and add individuals to datagrid

Medium Priority
405 Views
Last Modified: 2012-06-27
Never done this before and don't know where to start, I need to query active directory groups and then add the users that are in those groups to a datagrid.  How can I do this.  Can build the datagrid easy but know nothing about querying active directory from asp.net 1.1.  Please help
Thanks
Comment
Watch Question

Commented:
You want to lookup users from a given AD group?

Author

Commented:
Correct I need to be able to query 8 different active directory groups and pull out these users in a sql query and then put it into asp.net and populate a datagrid.  Once the query is built I can handle rest but don't know syntaxes to query AD.

Commented:
ok, just for starters, Google System.DirectoryServices. I am in the middle of something and cant write the code to test it. Read it, it has everything you are looking for and probably will have a solution even before I get to it.

-Suj
Commented:
HI,
Ok here is some code I have copied from net and changed to get working to my situation with help.  How can I take this procedure and create and overall Stored procedure and insert the temp table into permanent table.

create table #MemberOfGroups(
groepNaam varchar(400),
cn varchar(400),
displayName varchar(400))
SET NOCOUNT ON
declare @t varchar(100),@t2 varchar(1000), @ot varchar (4000), @tt varchar (4000);

declare gC cursor
for
select cn, distinguishedName
from openquery
(ADSI,'SELECT cn, distinguishedName
FROM ''LDAP://NGMNA1DCG
WHERE objectCategory = ''group''
and CN = ''NGMN SEC J6 Automation FTS''
or CN = ''NGMN SEC J6 Help Desk (Level 1)''
or CN = ''NGMN SEC J6 Help Desk (Level 2)''
or CN = ''NGMN SEC J4 Admins''
or CN = ''NGMN SEC J8 Admins''
or CN = ''NGMN SEC FMO Admins''
or CN = ''NGMN SEC RRM Admins''
or CN = ''MN Admins''
')
open gC

FETCH NEXT FROM gC INTO @t, @t2
WHILE @@FETCH_STATUS = 0
BEGIN
set @ot = '''SELECT cn, displayName
FROM ''''LDAP://NGMNA1D
WHERE objectCategory = ''''Person'''' AND objectClass = ''''user''''
AND memberOf=''''' + @t2 + '''''';
set @tt = 'select '+ ''''+@t+'''' +' As GroepNaam, cn, displayName from openquery(ADSI,'+ @ot +''') order by cn'

insert into #MemberOfGroups(groepNaam, cn, displayName)
EXEC (@tt)
--print @tt
FETCH NEXT FROM gC INTO @t, @t2
END
CLOSE gC
DEALLOCATE gC

select Distinct(CN), groepNaam as 'Group' from #MemberOfGroups Where lower(substring(CN, 1, 3)) <> 'svc' order by CN
--order by displayName

drop table #MemberOfGroups

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Hi,
Ok I found how to insert into permenent table by adding this to the method  I added it right before the Select statement at the bottom, don't know it its correct way to do but it works.  So teh questions i have left are
1.  How can I pull email with this statement as well
2. How can I turn into a sql query statement or a stored procedure that I can run via an asp.net program.

INSERT INTO [IAComplianceDotNet].[dbo].[tblADAccounts]
           ([strName]
           ,[strAccount])
Top Expert 2009

Commented:
Here's another way to do it all in C#.

Read all users:
http://www.codeproject.com/KB/system/everythingInAD.aspx

Add it to the database / query from the database to display it on the grid.
http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson01.aspx


Top Expert 2009

Commented:
Please don't delete this issue. It has some valuable information from experts that others may find useful.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.