Tuesday, April 17, 2012

Obtaining a List of Users from a SharePoint site via a SQL command

It's only a matter of time before your PM is going to come to you one day and ask you "so...who now has access to that site you set up about a year ago?".  Rather than having to root around the various groups and compiling the list by hand, the quick n' dirty solution to this problem is to run the following SQL command against your SharePoint content database.  This query will quickly obtain a list of users based on the SharePoint groups they belong which you can then slap it into an Excel spreadsheet for them:
SELECT DISTINCT [Groups].[Title] as 'Group', [UserInfo].[tp_Title] as 'User', [UserInfo].[tp_Email] as 'Email' FROM [Groups] INNER JOIN [GroupMembership] ON [GroupMembership].GroupId = [Groups].ID AND [GroupMembership].SiteID = [Groups].SiteID INNER JOIN [UserInfo] ON [UserInfo].tp_ID = [GroupMembership].MemberId AND [UserInfo].tp_SiteID = [GroupMembership].SiteID ORDER BY [Groups].[Title] ASC, [UserInfo].[tp_Title] ASC
By the way, please don't get the impression that I'm implying that you can write INSERT or DELETE statements against these databases.  The minute you do that is the minute that Microsoft will "disown" your SharePoint site and will not be able to provide technical support for it.  This information should be considered READ-ONLY to you and you should only add, modify, or delete users using the SharePoint interface.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.