Click here to Skip to main content
Licence CPOL
First Posted 23 Mar 2010
Views 7,686
Bookmarked 8 times

Use SQL to view Active Directory Data

By | 30 Mar 2010 | Technical Blog
If you want an easy way of viewing objects in Active Directory, one good way of doing it is creating a view or stored procedure on SQL Server.
 
Part of The SQL Zone sponsored by
See Also
A Technical Blog article. View original blog here.[^]

If you want an easy way of viewing objects in Active Directory, one good way of doing it is creating a view or stored procedure on SQL Server. By doing this, now you can easily call the views or stored procedures from your application without bothering to use System.DirectoryServices. The only downside is that it limits the resultset to 1000 records, not sure how to increase this as of the moment but this is good for selecting a dataset from AD which has common properties.

You need to do the following steps:

Step 1

Create a linked server to your Active Directory from SQL by running this command.

sp_addlinkedserver 'ADSI', 'Active Directory Service Interfaces', _
	'ADSDSOObject', 'adsdatasource'

‘ADSI’ can be any name you want.
After doing that, you will see the new linked server from the Server Objects – Linked Server.

Linked Server

Step 2

Adjust the security settings as needed. If you want to run the command as a specific user, then you have to right click on the linked server you just created and select properties, then go to the security tab where you can set the login and password of the account that will run the command.

Security Context

Step 3

You can now create you view or stored procedure. For this sample, we will create a stored procedure which has the sAMAccountName as the parameter. sAMAccountName is the login name of a specific user.

CREATE PROCEDURE [dbo].[ActiveDirectory_GetUserByUserName]
@sAMAccountName varchar(200)

AS
BEGIN

SET NOCOUNT ON;

declare @STRSQL varchar(8000);

select @STRSQL = 'SELECT
employeeNumber,
sAMAccountName,
displayName,
givenName,
sn,
initials,
name,
title,
company,
department,
facsimileTelephoneNumber,
telephoneNumber,
homePhone,
mobile
FROM OPENQUERY
(ADSI,
''SELECT
employeeNumber,
sAMAccountName,
displayName,
givenName,
sn,
initials,
name,
title,
company,
department,
facsimileTelephoneNumber,
telephoneNumber,
homePhone,
mobile
FROM ''''LDAP://DC=test,DC=com''''
where objectClass = ''''User'''' and objectCategory = ''''Person''''
and sAMAccountName = ''''' + @sAMAccountName + ''''''')'

EXEC(@STRSQL)
END 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Raymund Macaalay

Technical Lead

New Zealand New Zealand

Member

Follow on Twitter Follow on Twitter
http://nz.linkedin.com/in/macaalay
http://anyrest.wordpress.com/

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
General1000 Record Limit Pinmembershiggin23:08 30 Mar '10  
QuestionMust declare the scalar variable "@STRSQL" [modified] PinmemberSevententh22:31 29 Mar '10  
AnswerRe: Must declare the scalar variable "@STRSQL" PinmemberRaymund Macaalay9:22 30 Mar '10  
GeneralRe: Must declare the scalar variable "@STRSQL" PinmemberSevententh22:10 30 Mar '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 30 Mar 2010
Article Copyright 2010 by Raymund Macaalay
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid