SharePoint 2007 Employee Directory Web Part

SharePoint 2007 Employee Directory Web Part

Many of our clients have asked us about some type of function or Web Part that lets them show a list of users in their site collection, specifically from the "Users and Groups" list that they can control.  Out of the box MOSS gives you the Site Users Web Part.  This is often discarded by the client because it is too basic.  Here is what it looks like:

  clip_image001

We are generally asked if we can show profile pictures, some imported Active Directory profile information, sorting, links to the user display or My Site page.  In order to do this we chose to write a custom Web Part.

The point of this article is to show SharePoint developers an alternative method to using the Site Users Web Part as an employee directory.  We will show the basic steps to create your own Web Part to offer more information and flexibility while introducing you to some SharePoint functionality you may not be familiar with.

This article assumes you are familiar with Web Part development in SharePoint 2007 and already understand how to build and deploy a basic Web Part with a basic understanding of ASP.NET development.

 

The Code and the Process

Recently for a client we needed to pull user information from the root Site Collection to create a "Face Book".  Since users and permissions are Site Collection-based, this allows the local administrator to manage the list of users that will be visible in the Face Book.

We chose to use the SPList SiteUserInfoList to obtain this data.  We will go into the details of this list in another post, but for the purposes of this article it is the Site Collection-based user/permissions list.  We used the list located at the Site Collection's root Web:

clip_image001[6]

The SPList object does not support binding to a DataTable, but a hidden gem in WSS 3.0 is the ability to call SPList.Items.GetDataTable().  This returns an SPList object converted to a DataTable object which can more readily be used with grid controls.

We also chose to use the SPGridView instead of the standard GridView from .NET 2.0.  The version specific to SharePoint creates default formatting to match standard WSS 3.0 Web Parts.  One item on my wish list from this experience would be that an SPList could be bound to an SPGridView natively without requiring the conversion to a DataTable object.

In the OnInit() event handler we add to our control tree and this is where we set up all our UI Controls.  Below is a snippet of code that shows wiring up a DropDownList to handle filtering by Active Directory department, creating the SPGridView and adding a few different types of columns to the display grid.

clip_image001[11]

In the OnLoad() event of the page we simply call the Method to Load the Data.  We call this with the SPSecurity.RunWithElevatedPrivileges so that any data accessed with be run with administration privileges.

The Bind method looks like this:

clip_image001[5]

We iterate through the User DataTable in order to make some changes along the way.  SharePoint likes to store some data with delimited values in a single field.  It does so with the user's profile picture URL.  We have to split this value in order to assign it to an ImageField column.

Next it shows the limitation of the UserInformationList.  Without going into detail in this article, this data represents a small subset of the Profile Data you might expect to be available at the Site Collection level.  In order to get Profile data not available in the local context, we must load a UserProfile object to obtain the Mobile Phone data we needed to display.  Not the most elegant solution, but it worked.

At this point we assigned the DataView to the DataTable and give a standard RowFilter that removes records from the view that do not contain a last name.  This filters out generic accounts as opposed to legitimate Site Collection users.

Here is the rest:

clip_image001[7]

Next we're checking for a post back and essentially lining up the filtering and sorting so that it maintains state between posts.  The only interesting thing to note here was the need to select a distinct value from the UserInformatonList DataTable.  The only easy way I found to do it was by calling the DataTable.DefaultView.ToTable() method that allows you select distinct records as a boolean and on what columns.  I only needed department because this represented my DropDownList filter.

The remaining code represents the event handler methods to control sorting and filtering.  This is the same in SharePoint as it is in ASP.NET, so I don't think I need to post it.  Below is roughly how the Web Part looks rendered:

clip_image001[9]

I don't have a lot of users in this Site Collection, but you get the point about what an SPGridView looks like, the user data, picture, DropDownList, and sorting.  This should at least serve as a glimpse into what is possible.

-Ryan