Common Database Query Series - Users

I can't tell you how many times I'm asked "how do I get a list of unique users for my farm" or "who has access to this site". These requests were so common that I created a tool to dump a list of unique users per farm over the last 30 days (using the OM). It is also possible to view information about users in the content databases. In 2003 it usually took a complex series of SQL joins to get the data you wanted. I realized that I hadn't tried these same tasks in 2007 so naturally I started playing :-) What I found was that the schema has been greatly simplified in 2007. (at least in the user context) What follows are a few queries I wrote to get user information on the fly.

CAUTION: I'm no SQL expert. Use these queries at your own risk. Modifying your SharePoint databases directly will result in loss of support!

 This query dumps user title, email address and the url of the site they have access to into a temp database with which you can query later. There are other interesting attributes as well such as site administration status (tp_SiteAdmin = true) and Account Name (tp_Login)

Use <contentDBName)
GO

Select u.tp_title, u.tp_email, w.fullurl

into ##mytemp

from userinfo u, webs w

where u.tp_siteid = w.siteid and w.parentwebid is null

Run the above query once to create the temp table. Run the following query on each remaining content database.

Use <contentDBName)
GO

Insert into ##mytemp

Select u.tp_title, u.tp_email, w.fullurl

from userinfo u, webs w

where u.tp_siteid = w.siteid and w.parentwebid is null

Once you have inserted all your data into the temp table, you can manipulate the table as needed. This query returns the number of users permissioned on each individual site:

Select count (tp_title) as users, fullurl

FROM ##mytemp

Group By fullurl

This query returns the unique email addresses of permissioned users across all sites (removes duplicates for communication purposes)

Select distinct tp_email

from ##mytemp

Order By tp_email ASC

Something the databases won't tell you is if these users have actually visited a site in the last 30 days. There's an IsActive attribute, but I think this only tells you if the user has been removed from the profile database. To find actually active users, use the OM's web getusage method. You can create a tool to do this or use Powershell.