SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

How to Display an AD group in a SharePoint DataView

 

I've come across the occasion, once or twice, where the customer wanted to be able to display all the users of a particular Active Directory group in a SharePoint list.  Though a seemingly simple request, it quickly turned into a headache for me.  Couldn't I use Audiences or a similar resource to populate a list?  I finally came up with this solution.  Although it's something of a kludge, it works reliably once it's set up.

 

    It is possible to query Active Directory directly, as if it was a database server.  Setting this up is somewhat complex, but most steps only need to be performed once.

     

    Resources you'll need:

    • An account with Read Access to Active Directory
      • This account will be reading AD on behalf of the query
    • SQL server 2005
      • You will be working in SSMS
        • You will need to have permission to create a linked server, create a database, and create objects in the database.
    • SharePoint Designer 2007
      • SPD is required to be able to create data views on SharePoint pages.  No other tool can do this.
    • Authoring access to the SharePoint page(s)

     

  • Part One: Using SQL Server Management Studio, perform the following steps:
    • First, we need to set up linked server access to AD:

     

    --Add a linked server which will connect to Active Directory Services (ADSI)

    --This only needs to be done once.

    sp_addlinkedserver 'ADSI', 'Active Directory Service Interfaces',

    'ADSDSOObject', 'adsdatasource'

    GO

     

    Note: you may need to modify this Linked Server as follows:

    First, go to the Linked Server Properties:

    Linked Server Properties

     

    Next, specify that the connection will be made using a specific context.  In this case we used the Domain Administrator, but really any account with Read Access to the AD schema would be fine.

    Linked Server Credentials

     

     

  • Part Two: Create Database, View, and SQL Login
    • Create a database
      • To host the views we'll be running against Active Directory, we'll need to set up a database.  You can use an existing one if you want, but I prefer to keep these items separate from my other production data. 
      • I use the name ADLookup.
      • The database doesn't need to be anything fancy.  Just accept defaults and continue. 

    New Database Properties

    • Create a SQL Login
      • In this example I created an account named "ADLookup", with the following options:
        • Authentication type: SQL Server
          • Note: this solution depends on the SQL security being set to Mixed Mode.  If you are at a site that requires Windows Only authentication I don't think this will work for you.  There is probably an alternative method, but I haven't looked into it yet.
        • Default Database: ADLookup
        • Server Roles: None
        • User Mapping:
          • ADLookup: Database Owner

     

     

    • Create a view to do the work for us.  All items in RED can be changed to suit your purposes.

     

    -- Now show the login name and display name for all users in the specified group.

    USE [adlookup] --This is the user database to hold all of the views we'll be creating

    GO

     

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    /*Show the login name and display name for all users in the specified group.*/

    CREATE VIEW [dbo].[vw_groupname]

    AS

    SELECT     sAMAccountName AS [Login Name], givenName AS [First Name], sn AS [Last Name], displayName AS [Full Name], mail AS [E-Mail Address],

                          telephoneNumber AS Phone

    FROM         OPENQUERY(ADSI,

                          'SELECT sAMAccountName, givenName, sn,displayName, mail, telephoneNumber

    FROM

    ''LDAP://dc=YourDomain,dc=local''

    WHERE objectClass = ''user'' and objectCategory=''person''

    AND memberOf = ''CN=YourGroup,OU=YourOU, dc=YourDomain,dc=local''')

                           AS derivedtbl_1

    • Notes:
      • This will return all members of the "YourGroup" group, located at "YourDomain.local/YourOU/YourGroup"
      • The "objectCategory" parameter is to differentiate between users and computers (which can also be members of a group).
      • This query can then be copied into a view, which will dynamically return all users in a given group.
      • Then SharePoint can use this view to populate a Data View.  This workaround allows us to show all the users who are members of the group and display it on the page, something SharePoint can't seem to do by itself.
    • Customization:
      • Specify the view name
        • Use the naming convention: vw_GroupName (e.g. vw_YourGroup)
        • In this example I use a view named vw_AnchorageStaff.
        • This should match the name of the AD group, to avoid confusion
      • Specify the properties to be queried per user in the group.
        • The above query returns the following properties:

     

    sAMAccountName  AS [Login Name],

    givenName AS [First Name],

    sn AS [Last Name],

    displayName AS [Full Name],

    mail AS [E-Mail Address],

    telephoneNumber AS Phone

     

        • You will want to "rename" each of the columns as something more readable.  Column names with spaces have to be enclosed in brackets.
        • Active Directory has an enormous number of properties that can be queried.  You can google the Microsoft site for a list of them.
        • All properties are case-sensitive!
      • Specify the OpenQuery statement. 
        • This is essentially a query within a query.  The OpenQuery statement is querying Active Directory to build a resultset, and then our Select statement is querying that resultset.  This means you're running the same query twice.
        • Because we're running two identical queries, they need to be exactly the same columns in the same order.
        • The placement of single and double quotes is imperative.  Be sure to keep them exactly as they are.
        • Modify the AD location of the group you're querying:
          • ADSI isn't smart enough to search for the group.  You'll need to specify its location.
    • Run the view:
      • It should return the right number of records and columns.

    Linked Server Resultset

     

     

  • Part Three: Using SharePoint Designer, create a Data View
    • In SPD, go to File-->Open Site and browse to the site you wish to work with.
    • You may wish to update an existing page or create a new page. 
    • Click in an available area on the SharePoint page.
    • On the page to be edited, click Insert-->Data View

    Insert Dataview

 

    • A DataView control is added.

    Data View added

    • In the Data Source Library tab of the task pane, click Connect to a Database.

Connect to a Database

    • In the Data Source Connection dialog, click Configure Database Connection.

    Configure DB connection

 

    • In the Configure Database Connection dialog, enter connection information for your SQL server.  In this case I am connecting to an instance named "Sharepoint".

    Configure SQL connection

      • Note: the ADLOOKUP user is a SQL user and needs to have (at least) read access to all views in the ADLOOKUP database.

 

    • Click OK to acknowledge that passwords are passed in clear text.

    Security warning 

    • Select the name of the view we just created, and click Finish.

    Select View

    • In the Data Source Properties dialog, click the Fields button to select which fields to include in this view.

    • Move fields over to add or remove them from the view.

Move fields over to include

    • In the Data Source Library pane, click the drop down arrow next to the view we just added and select "Show Data".

    Show Data

    • The Data Source Details tab is activated and you see the fields available to be put on the page.

Data Source Details tab

    • Select all the fields, then click the Insert Selected Items As… Button.  For this example we'll use a Multiple Item View.

    Insert the selected fields as a Multiple Item View

     

    • The fields are then populated in the data view on the page.

    • You will need to customize the DataView from here:
      • Change column names
        • You'll note that any spaces turn into codes ("_x0020_").  You can modify this by clicking on each column header and editing the text.
      • Change pagination
        • Long lists take a while to load.  Don't go crazy.
      • Add buttons for sorting and grouping
      • Changing list style
    • To customize the DataView, click on the small chevron button at the top right of the list.
      • Click Data View Properties
      • There are tabs for general, layout, and paging

     

     

    This should get you started! 

     


Posted 01-10-2008 1:20 PM by moffitar

Comments

Links (1/10/2007) « Steve Pietrek’s SharePoint Stuff wrote Links (1/10/2007) « Steve Pietrek’s SharePoint Stuff
on 01-10-2008 7:22 PM

Pingback from  Links (1/10/2007) « Steve Pietrek’s SharePoint Stuff

Kathy wrote re: How to Display an AD group in a SharePoint DataView
on 01-11-2008 10:01 AM

You sound like you know what you're doing...so I'm going to give you a challenge that even some of the Microsoft 'Experts' haven't been able to answer.

We have SharePoint and Business Portal and when I added a Page Viewer web part, I wanted it to automatically show a user's program icons that are currently on their desktop. This would allow them to work from within the home page. It originally worked, but since we've upgraded some of the PC's to XP, the web part no longer shows the icons. I have since figured out that if I start the path with the computer's name, that that will finally work. However, I need the syntax for the path to figure out the current user's computer name and computer profile name.

Here's an example of what I currently have: \\mycomputersname\c$\Document and Settings\usersname\Desktop. Of course, if I fill in my actual computer's name and my own profile name, then my icons appear.

Thanks for reading, even if you can't help--

Kathy--

moffitar wrote re: How to Display an AD group in a SharePoint DataView
on 01-11-2008 1:09 PM

Kathy,

If Microsoft hasn't come up with a solution for you, I am certain my suggestions will seem trite to you.  However, I will give it a whack.

1. Your UNC path to the user's desktop uses the administrative share for the C: drive (i.e. C$).  By default this is not available to anyone except local administrators on the workstation.  You might work around this by creating an alternate share to the user's desktop.  That is, in the user's Documents and Settings folder, right click on the Desktop folder, and create a share.  Then the UNC would be something like this: \\computername\UserDesktopShare.  

2. The Windows XP SP2 firewall is fairly aggressive in closing off remote access to workstations.  If you moved from Windows 2000 to XP, you've likely seen some of this.  For an experiment, try turning off the Windows Firewall/ICS service on the user's workstation to see if it makes any difference.  

3. Depending on what antivirus / antispyware / antimalware product you're using, this may also be blocking access.  Experiment with turning those off, too.

Of course, I don't need to point out that thwarting workstation security like this is a big no-no.  Probably the best solution for you would be option #1.  You can tighten security by only allowing a particular user access to the share.

Of course, the more users you have, the bigger headache it becomes, creating shares for every desktop for every profile on every machine.  Or else you could make everyone a local administrator.  I wouldn't do that, though.

Hope that helps!

moffitar wrote re: How to Display an AD group in a SharePoint DataView
on 01-11-2008 1:20 PM

Kathy,

OK, I went back and reread your question.  I think you're saying you need a way to automatically determine the logged-in user's machine name, then dynamically map a UNC path back to their desktop.

That's a little trickier.

Without delving into some code, here is a possible kludge.  

Depending on which application we're talking about (e.g. MS Word, Quickbooks, etc) you may be able to depend on the fact that most programs get installed to their default locations.  So you can copy the Shortcut icons on a typical machine to a network share (let's say, \\Server\AllUsers\Shortcuts) and then point your UNC paths there.  The shortcuts will still be pointing to local paths.  It should work for most users.  For the odd user whose programs are in different locations, you could create an alternate web part and, using audience targeting, display an alternate collection of shortcuts (for example, to \\Server\OddUser1\Shortcuts).

moffitar wrote re: How to Display an AD group in a SharePoint DataView
on 01-11-2008 1:27 PM

OK last post :)

You could also try using a UNC like this:

\\%computername%\c$\Document and Settings\%username%\Desktop.  

For example, if you go to a command prompt and type:

echo \\%computername%\c$\Document and Settings\%username%\Desktop , it will fill in the variables for you.  

But I'm not sure that variables are allowed in UNC paths.  If I type the above into an address bar, it doesn't seem to work.

Anyway, I'm out of ideas.  HTH.

Bo wrote re: How to Display an AD group in a SharePoint DataView
on 01-16-2008 7:16 AM

Hi moffitar,

Good post, very useful information.  I do have one question though:  We are in a multi-server environment, how do I make the connection string on the SQLDatasource configurable?  Will I have to re-edit every page with this type of connection?

Thanks!

moffitar wrote re: How to Display an AD group in a SharePoint DataView
on 01-22-2008 1:07 AM

Hi Bo,

If you're using my method, then yes, that's what you'll have to do.  I did say it was a kludge.

Some thoughts on the matter, however:

1. This method could probably be encapsulated in a web part with not much effort.  I haven't dabbled much in that area (more of a factor of being busy than anything else).  You might Google around to see if anyone else has been working on that, or see if you can tempt a developer to do it for you.

2. I don't have my test server with me right now, or I would verify this, but I've set up DataViews in the past that could reference Sharepoint lists and use them as filtering criteria.  It might be possible to play with this aspect to create a generic DataView that could display different data based on the value of a field elsewhere on the site.

3. Sharepoint does have the ODC Library; the Office Data Connections can be pre-set and stored in Sharepoint.  Again, not much experience here, but it seems you could simply re-point the DataView to a new ODC.  But I still think you'd need SPD to edit the DataVIew properties.  You'll want to verify this.

Hope that helps!

SharePoint 2007 How To List « SharePoint Sherpa wrote SharePoint 2007 How To List « SharePoint Sherpa
on 02-12-2008 7:29 PM

Pingback from  SharePoint 2007 How To List « SharePoint Sherpa

W2 wrote re: How to Display an AD group in a SharePoint DataView
on 02-20-2008 2:02 PM

Hi moffitar,

How would I be able to use the data view to drill down to the member level of each group?

Thanks!

Jason wrote re: How to Display an AD group in a SharePoint DataView
on 04-16-2008 4:11 PM

This is great and I am wondering how I could expand it...I want to be able to dynamically pull in all of the groups in a certain OU and allow the user to select the group they want to show members of in the data view web part.  is this possible?

Owen wrote re: How to Display an AD group in a SharePoint DataView
on 07-01-2008 8:43 AM

Any chance you can be a bit more specific on how exactly I setup the linked server access to AD in Part One: Using SQL Server Management Studio, as I'm not too familiar with this tool.

Thanks

Owen

Hi wrote re: How to Display an AD group in a SharePoint DataView
on 08-26-2008 9:16 AM

Hi,

  I m following ur article to pull the data from AD in a sharepoint list.I Followed all the above instruction accordingly.When i trying to execute a view

CREATE VIEW [dbo].[vw_ADGroup]

AS

SELECT sAMAccountName AS [Login Name], givenName AS [First Name], sn AS [Last Name], displayName AS [Full Name], mail AS [E-Mail Address],telephoneNumber AS Phone

FROM OPENQUERY(ADSI, 'SELECT sAMAccountName, givenName, sn,displayName, mail, telephoneNumber FROM ''LDAP://dc=SHAREPOINTSERVE,dc=local''

WHERE objectClass = ''user'' and objectCategory=''person''

AND memberOf = ''CN=Administrators,OU=mhussain, dc=SHAREPOINTSERVE,dc=local''')

                      AS derivedtbl_1

I m getting this error : Could not open a connection to SQL.

Please do let me know where i m going wrong.Do my connection string in view is up to the way.

Thanks

moffitar wrote re: How to Display an AD group in a SharePoint DataView
on 09-15-2008 5:19 PM

W2,

What you are asking to do is possible.  I've done it before, but I used ASP.NET code to build a form and drill down; each time the user expanded a node I would build another query and send it to the back end.  To SQL Server, LDAP is just a database, so it shouldn't behave much differently.

Jason,

Same answer applies.

Owen,

The process is fairly straightforward if you understand the concepts.  Here's a good link:  www.databasejournal.com/.../3691721

Note that Gregory Larsen's walkthrough shows you how to use the GUI to configure the linked server; I used a SQL command to do the same thing.  Just make sure you have the right properties so you'll connect properly: ADSI, Active Directory Services Interfaces, ADSDSOObject, asadatasource.

Hi,

I think the ''LDAP://dc=SHAREPOINTSERVE,dc=local'' might be the problem here.  Is this the name of your server, or your domain?  The DC= value has to be the name of your domain.   Example: DC=Contoso, DC=Local.

Achito wrote re: How to Display an AD group in a SharePoint DataView
on 12-04-2008 8:09 AM

Moffitar,

I just wanted to say thanks for posting this. I am in the process of creating a Lookup Field for all the computers in the domain, and this solution looks like it might just be the answer I needed.

-Achito

Gaurav Saxena wrote re: How to Display an AD group in a SharePoint DataView
on 01-11-2009 11:44 PM

Hi,

My Requirement is like this:

I have to fetch all the groups for a given user.

sharepointfan wrote re: How to Display an AD group in a SharePoint DataView
on 01-13-2009 9:52 PM

Great blog, I learn a lot. Thank you very much.

I am using sharepoint AD Information Sync from www.sharepointboost.com to do those  kind of thing now.Before I read your blog I thought it would give me solution to my trouble, but it did not mentioned. MY TROUBLE is half resolved. What remains is : In your way I can not sync those information.

Do you have any clue?

Thank again for you great post!

Allen Price wrote re: How to Display an AD group in a SharePoint DataView
on 03-03-2009 12:50 PM

Is there any way to use Sharepoint to add and remove users to AD groups?

moffitar wrote re: How to Display an AD group in a SharePoint DataView
on 03-03-2009 2:14 PM

Guarev:

I don't have my reference material in front of me, but it seems to me that you would want to look at the MemberOf collection for a given user.  Sorry I can't be more specific, but I know it's at least doable.

SharePointFan:

If you're looking to update properties on an AD user's site collection profile (or MySite profile), and have those properties pushed back to update Active Directory, then I don't know of any way to do this.  it's not an out of the box feature, and something of a security risk.  I don't know of any products that will do this, either.  Sorry.

Allen:

Well, out of the box, I'd say no.  You might look at Nintex Workflow, though (http://www.nintex.com) who have done some amazing things with workflow, including the ability to automate the creation of AD users and groups within a workflow process.  Slicker than snot.  Kinda pricey, though.

Mayaa wrote re: How to Display an AD group in a SharePoint DataView
on 04-21-2009 7:45 AM

Hello there,

I wanted to try your solution in wss3.0

doest work in wss3.0??

Thanks?

moffitar wrote re: How to Display an AD group in a SharePoint DataView
on 04-21-2009 11:25 AM

Mayaa,

Yes, it should work the same in WSS 3.0, since it is using a Data View Web Part and not any component that requires MOSS.

Ra88 wrote re: How to Display an AD group in a SharePoint DataView
on 04-23-2009 12:21 PM

OMG, this saved me! Thank you!!! It works great in WSS 3.0 and SQL 2008!

SELECT sn AS LastName, givenName AS Firstname, displayName AS FullName, telephoneNumber, mail, physicalDeliveryOfficeName AS Office

FROM OPENQUERY(ADSI, 'SELECT givenName, sn, sAMAccountName, displayName, mail, telephoneNumber, mobile, physicalDeliveryOfficeName, department, division  

FROM ''LDAP://domaincontroller.domain.local''

WHERE objectCategory = ''Person'' AND objectClass = ''user''')

AS derivedtbl_1

WHERE     (sn IS NOT NULL) AND (mail IS NOT NULL)

ORDER BY Office, LastName

Be sure your SQL server is in mixed authentication mode. I couldn't get it to work with Windows authentication in SP Designer, but I could in Visual Studio 2008 for some reason.

Create your read-only user account by right-clicking Security>Logins. The under Security of the ADLookup DB, give the account db_datareader under Role Members

Alston wrote re: How to Display an AD group in a SharePoint DataView
on 06-02-2009 8:40 AM

Hi. The function of science fiction is not always to predict the future but sometimes to prevent it. Help me! Could you help me find sites on the: Payday loan. I found only this - <a href="http://payday-on-line.biz">payday loan online</a>. Allergic inammation is in local allergic reactions. Shop for unique anti allergic designs on magnets. With respect :-(, Alston from Lebanon.

Mike wrote re: How to Display an AD group in a SharePoint DataView
on 06-08-2009 7:52 PM

Hi moffitar,

Great post!!!  Thanks for posted it...

I have a question for you, I was able to create the view, but for some reason it doesn't pull the information from AD. When I open the view I only see NULL in all the fileds.

Here is my script:

CREATE VIEW [dbo].[vw_dl_bas_it]

AS

SELECT sAMAccountName AS [Login Name], givenName AS [First Name], sn AS [Last Name], displayName AS [Full Name], mail AS [E-Mail Address], telephoneNumber AS Phone

FROM OPENQUERY(ADSI,'SELECT sAMAccountName, givenName, sn,displayName, mail, telephoneNumber FROM

                    ''LDAP://dc=MyDomain,dc=local'' WHERE objectClass = ''user'' and objectCategory=''person''

                    AND memberOf = ''CN=DL BAS IT,OU=New OU Structure, dc=MyDomain,dc=local''') AS derivedtbl_1

Could be because the group "DL BAS IT" is located at MyDomain.local/New OU Structure/Distribution Lists

Would you please tell me where I made the misstake? or what is missing?

Thanks in advance for your help and I look forward to hearing from you,

Mike

moffitar wrote re: How to Display an AD group in a SharePoint DataView
on 06-09-2009 1:59 PM

Hi Mike,

I don't have my lab server running so I can't really test your script, sorry.  However:

Three things occur to me.

1. Is the group a "Distribution List" or is it a "Security Group"?  This may make a difference if AD sees them as different classes of object.

2. Try removing the AND clause to see if this is the criteria that is breaking the query.  If it does return results, then you know that this is the problem.  If so, try pointing to a different group (a security group) and see if it works.  

3. My AD objects did not contain any spaces.  Yours do.  This might make a difference, but probably not.  In T-SQL, this problem is solved by enclosing the values in [square brackets], but I am not sure how this would work in an LDAP query.  Might give that a try, though.  

Post back here and let me know if it works!

Mike wrote re: How to Display an AD group in a SharePoint DataView
on 06-09-2009 6:19 PM

Hi moffitar,

Thanks! it is working now.

I have added the name of the subfolder where all AD groups are => ''CN=DL BAS IT,OU=Distribution Lists, OU=New OU Structure, dc=MyDomain,dc=local'''

But now I'm stuck in SPD 2007, I'm getting an error message, when I'm trying to connect to the Database, the error says: " Server error: The server for the data source returned a non specific error when trying to execute your query" and it doesn't show the fields.

I have checked the SQL login-user and it has access to the database, I'm able to select the DB and I see the view, but when I click in Finish I got that error message and it doesn't show the fields of the view.

Thanks in advance for your help and I look forward to hearing from you,

Mike

Mike wrote re: How to Display an AD group in a SharePoint DataView
on 06-09-2009 7:32 PM

Hi moffitar,

Please disregard my previous post.

I deleted my Sql user and created it again and it worked.

Thanks,

Mike

Mike wrote re: How to Display an AD group in a SharePoint DataView
on 06-09-2009 8:25 PM

moffitar,

Great blog!!! now my goal is to have a page that allow users to select an specific AD group and show all its members and who is the owner/manager of that group, so they can request to them to be added on that group.

Again thank you!!!

Mike

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Need SharePoint Training? Attend a SharePoint Bootcamp!
Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.