in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

wsspectacular

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! 

     

Comments

 

Links (1/10/2007) « Steve Pietrek’s SharePoint Stuff said:

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

January 10, 2008 7:22 PM
 

Kathy said:

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--

January 11, 2008 10:01 AM
 

moffitar said:

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!

January 11, 2008 1:09 PM
 

moffitar said:

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).

January 11, 2008 1:20 PM
 

moffitar said:

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.

January 11, 2008 1:27 PM
 

Bo said:

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!

January 16, 2008 7:16 AM
 

moffitar said:

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!

January 22, 2008 1:07 AM
 

SharePoint 2007 How To List « SharePoint Sherpa said:

Pingback from  SharePoint 2007 How To List « SharePoint Sherpa

February 12, 2008 7:29 PM
 

W2 said:

Hi moffitar,

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

Thanks!

February 20, 2008 2:02 PM
 

Jason said:

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?

April 16, 2008 4:11 PM
 

Owen said:

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

July 1, 2008 8:43 AM
 

Hi said:

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

August 26, 2008 9:16 AM

Leave a Comment

(required )  
(optional )
(required )  
Add

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts