Since it is not recommended to undergo direct execution of SQL Queries on SharePoint content database, I would strictly recommend you to first go through one of my earlier articles “Executing Direct SQL Queries on SharePoint Content Databases: Is it a good idea???” talking about the recommended practices and consequences of direct execution.
With the assumption that you are already aware of the consequences of executing the direct SQL queries on SharePoint content databases, I would like to present the following important queries that can be used to retrieve SharePoint Vitals directly from the content databases, which would otherwise be very difficult to get from SharePoint Programming APIs.
These queries are tested with SharePoint 2013 and a few of them might not work with the earlier versions of SharePoint due to changes in database schema.
Returns Total Number of Site Collections in a Web Application
Query
- SELECT COUNT(*) AS ‘TOTAL SITE COLLECTION’ FROM SITES
Output
Returns Root Site Title for each Site Collection available in WebApplication
Query
- SELECT TITLE AS ‘ROOT WEB TITLE’, SITES.ROOTWEBID, SITES.ID AS ‘SITE COLLECTION ID’ FROM WEBS INNER JOIN SITES ON WEBS.ID = SITES.ROOTWEBID
Output
Returns Total Web Sites in Web Application
Query
- SELECT COUNT(*) As “Webs Count” FROM WEBS
Output
Returns Site Title and Site Id
Query
- SELECT TITLE AS ‘SITE TITLE’,FULLURL, SITEID AS ‘SITE COLLECTION ID’ FROM WEBS ORDER BY SITEID
Output
Returns Total number of Web Sites under each Site Collection
Query
- SELECT SITEID, COUNT(*) AS ‘TOTAL SUB SITES’ FROM WEBS INNER JOIN SITES ON SITES.ID = WEBS.SITEID GROUP BY SITEID
Output
Returns total number of Websites under each Site Collection for ‘Doc Lib’ and ‘Form Lib’
Query
- SELECT
- “TEMPLATE TYPE” = CASE WHEN [LISTS].[TP_SERVERTEMPLATE] = 101 THEN ‘DOC LIB’ WHEN [LISTS].[TP_SERVERTEMPLATE] = 115 THEN ‘FORM LIB’ ELSE ‘UNKNOWN’ END,
- “LIST URL” = ‘http :// win - etmg052h5r / ’;
- + CASE WHEN [WEBS].[FULLURL] = ” THEN [WEBS].[FULLURL] + [LISTS].[TP_TITLE] ELSE [WEBS].[FULLURL] + ‘ / ’ + [LISTS].[TP_TITLE] END,
- “TEMPLATE URL” = ‘http :// win - etmg052h5r / ’;
- + [DOCS].[DIRNAME] + ‘ / ’ + [DOCS].[LEAFNAME]
- FROM
- [LISTS]
- LEFT OUTER JOIN [DOCS] ON [LISTS].[TP_TEMPLATE] = [DOCS].[ID],
- [WEBS]
- WHERE
- (
- [LISTS].[TP_SERVERTEMPLATE] = 101
- OR [LISTS].[TP_SERVERTEMPLATE] = 115
- )
- AND [LISTS].[TP_WEBID] = [WEBS].[ID]
- ORDER BY
- “LIST URL”
Output
Returns count of documents from site collection
Query
- SELECT SUM(ITEMCOUNT) AS [TOTAL ITEM] FROM LISTS INNER JOIN WEBS ON LISTS.TP_WEBID = WEBS.ID INNER JOIN ALLLISTSAUX ON LISTS.TP_ID = ALLLISTSAUX.LISTID WHERE TP_SERVERTEMPLATE = 101
Output
Returns items count for each Document Library (101)
Query
- SELECT
- CASE WHEN WEBS.FULLURL = ” THEN ‘PORTAL SITE’ ELSE WEBS.FULLURL END AS [SITE RELATIVE URL],
- WEBS.TITLE AS [SITE TITLE],
- LISTS.TP_TITLE AS TITLE,
- TP_DESCRIPTION AS DESCRIPTION,
- ITEMCOUNT AS [TOTAL ITEM]
- FROM
- LISTS
- INNER JOIN WEBS ON LISTS.TP_WEBID = WEBS.ID
- INNER JOIN ALLLISTSAUX ON LISTS.TP_ID = ALLLISTSAUX.LISTID
- WHERE
- TP_SERVERTEMPLATE = 101
- ORDER BY
- [SITE RELATIVE URL]
Output
Returns all the top level site collections
Query
- SELECT SiteId AS SiteGuid, Id AS WebGuid, FullUrl AS Url, Title, Author, TimeCreated FROM dbo.Webs WHERE (ParentWebId IS NULL)
Output
Returns all the child sites in a site collection
Query
- SELECT SiteId AS SiteGuid, Id AS WebGuid, FullUrl AS Url, Title, Author, TimeCreated FROM dbo.Webs WHERE (NOT (ParentWebId IS NULL))
Output
Returns all SharePoint groups in a site collection
Query
- SELECT dbo.Webs.SiteId, dbo.Webs.Id, dbo.Webs.FullUrl, dbo.Webs.Title, dbo.Groups.ID AS Expr1,dbo.Groups.Title AS Expr2, dbo.Groups.Description FROM dbo.Groups INNER JOIN dbo.Webs ON dbo.Groups.SiteId = dbo.Webs.SiteId
Output
Returns all the users in a site collection
Query
- SELECT
- dbo.Webs.SiteId,
- dbo.Webs.Id,
- dbo.Webs.FullUrl,
- dbo.Webs.Title,
- bo.UserInfo.tp_ID,
- dbo.UserInfo.tp_DomainGroup,
- dbo.UserInfo.tp_SiteAdmin,
- dbo.UserInfo.tp_Title,
- dbo.UserInfo.tp_Email
- FROM
- dbo.UserInfo
- INNER JOIN dbo.Webs ON dbo.UserInfo.tp_SiteID = dbo.Webs.SiteId
Output
Returns all the members of SharePoint groups
Query
- SELECT
- dbo.Groups.ID,
- dbo.Groups.Title,
- dbo.UserInfo.tp_Title,
- dbo.UserInfo.tp_Login
- FROM
- dbo.GroupMembership
- INNER JOIN dbo.Groups ON dbo.GroupMembership.SiteId = dbo.Groups.SiteId
- INNER JOIN dbo.UserInfo ON dbo.GroupMembership.MemberId = dbo.UserInfo.tp_ID
Output
Returns all the sites where a specific feature is activated
Query
- SELECT
- dbo.Webs.Id AS WebGuid,
- dbo.Webs.Title AS WebTitle,
- dbo.Webs.FullUrl AS WebUrl,
- dbo.Features.FeatureId,
- dbo.Features.TimeActivated
- FROM
- dbo.Features
- INNER JOIN dbo.Webs ON dbo.Features.SiteId = dbo.Webs.SiteId
- AND dbo.Features.WebId = dbo.Webs.Id
- WHERE
- (
- dbo.Features.FeatureId = ’00BFEA71 - D1CE - 42de - 9C63 - A44004CE0104′
- )
Output
Returns all the users assigned to the roles
Query
- SELECT
- dbo.Webs.Id,
- dbo.Webs.Title,
- dbo.Webs.FullUrl,
- dbo.Roles.RoleId,
- dbo.Roles.Title AS RoleTitle,
- dbo.UserInfo.tp_Title,
- dbo.UserInfo.tp_Login
- FROM
- dbo.RoleAssignment
- INNER JOIN dbo.Roles ON dbo.RoleAssignment.SiteId = dbo.Roles.SiteId
- AND dbo.RoleAssignment.RoleId = dbo.Roles.RoleId
- INNER JOIN dbo.Webs ON dbo.Roles.SiteId = dbo.Webs.SiteId
- AND dbo.Roles.WebId = dbo.Webs.Id
- INNER JOIN dbo.UserInfo ON dbo.RoleAssignment.PrincipalId = dbo.UserInfo.tp_ID
Output
Returns all SharePoint groups assigned to the roles
Query
- SELECT
- dbo.Webs.Id,
- dbo.Webs.Title,
- dbo.Webs.FullUrl,
- dbo.Roles.RoleId,
- dbo.Roles.Title AS RoleTitle,
- dbo.Groups.Title AS GroupName
- FROM
- dbo.RoleAssignment
- INNER JOIN dbo.Roles ON dbo.RoleAssignment.SiteId = dbo.Roles.SiteId
- AND dbo.RoleAssignment.RoleId = dbo.Roles.RoleId
- INNER JOIN dbo.Webs ON dbo.Roles.SiteId = dbo.Webs.SiteId
- AND dbo.Roles.WebId = dbo.Webs.Id
- INNER JOIN dbo.Groups ON dbo.RoleAssignment.SiteId = dbo.Groups.SiteId
- AND dbo.RoleAssignment.PrincipalId = dbo.Groups.ID
Output
Returns all the users assigned to the roles
Query
- SELECT
- DISTINCT CASE WHEN PATINDEX(‘ % \ % ’, FullUrl) > 0 THEN LEFT(
- FullUrl,
- PATINDEX(‘ % \ % ’, FullUrl) – 1
- ) ELSE FullUrl END AS [Site],
- Webs.Title,
- Webs.FullUrl,
- Perms.ScopeUrl,
- UserInfo.tp_Login As Account,
- CASE WHEN UserInfo.tp_DomainGroup > 0 THEN NULL ELSE UserInfo.tp_Title END AS Username,
- CASE WHEN UserInfo.tp_DomainGroup > 0 THEN UserInfo.tp_Login ELSE NULL END AS [AD Group],
- NULL AS [SharePoint Group],
- Roles.Title AS RoleTitle,
- Roles.PermMask
- FROM
- dbo.RoleAssignment
- INNER JOIN dbo.UserInfo ON RoleAssignment.SiteId = UserInfo.tp_SiteID
- AND UserInfo.tp_ID = RoleAssignment.PrincipalId
- INNER JOIN dbo.Perms ON Perms.SiteId = RoleAssignment.SiteId
- AND Perms.ScopeId = RoleAssignment.ScopeId
- INNER JOIN dbo.Roles ON RoleAssignment.SiteId = Roles.SiteId
- AND RoleAssignment.RoleId = Roles.RoleId
- INNER JOIN dbo.Webs ON Roles.SiteId = Webs.SiteId
- AND Roles.WebId = Webs.Id
- WHERE
- Roles.Type <> 1
- AND tp_Deleted = 0
Output
Returns all the SharePoint groups assigned to the roles
Query
- SELECT
- DISTINCT CASE WHEN PATINDEX(‘ % \ % ’, FullUrl) > 0 THEN LEFT(
- FullUrl,
- PATINDEX(‘ % \ % ’, FullUrl) – 1
- ) ELSE FullUrl END AS [Site],
- Webs.Title,
- Webs.FullUrl,
- Perms.ScopeUrl,
- UserInfo.tp_Login As Account,
- CASE WHEN UserInfo.tp_DomainGroup > 0 THEN NULL ELSE UserInfo.tp_Title END AS Username,
- CASE WHEN UserInfo.tp_DomainGroup > 0 THEN UserInfo.tp_Login ELSE NULL END AS [AD Group],
- Groups.Title AS [SharePoint Group],
- Roles.Title AS RoleTitle,
- Roles.PermMask
- FROM
- dbo.RoleAssignment
- INNER JOIN dbo.Roles ON RoleAssignment.SiteId = Roles.SiteId
- AND RoleAssignment.RoleId = Roles.RoleId
- INNER JOIN dbo.Perms ON Perms.SiteId = RoleAssignment.SiteId
- AND Perms.ScopeId = RoleAssignment.ScopeId
- INNER JOIN dbo.Webs ON Roles.SiteId = Webs.SiteId
- AND Roles.WebId = Webs.Id
- INNER JOIN dbo.Groups ON RoleAssignment.SiteId = Groups.SiteId
- AND RoleAssignment.PrincipalId = Groups.ID
- INNER JOIN dbo.GroupMembership ON GroupMembership.SiteId = Groups.SiteId
- AND GroupMembership.GroupId = Groups.ID
- INNER JOIN dbo.UserInfo ON GroupMembership.SiteId = UserInfo.tp_SiteID
- AND GroupMembership.MemberId = UserInfo.tp_ID
- WHERE
- Roles.Type <> 1
- AND tp_Deleted = 0
Output
Returns all the documents from all the lists available in WebApplication
Query
- SELECT
- AllDocs.Leafname AS FileName,
- AllDOcs.Dirname AS “Folder Path”,
- AllLists.tp_Title AS “List Title”,
- Webs.Title AS “Web Title”
- FROM
- AllDocs
- JOIN AllLists ON AllLists.tp_Id = AllDocs.ListId
- JOIN Webs ON Webs.Id = AllLists.tp_WebId
- ORDER BY
- webs.title
Output
Returns master pages in Web Application for all the Websites
Query
- SELECT
- AllDocs.Leafname AS FileName,
- AllDocs.Dirname AS “Folder Path”,
- AllLists.tp_Title AS “List Title”,
- Webs.Title AS “Web Title”
- FROM
- AllDocs
- JOIN AllLists ON AllLists.tp_Id = AllDocs.ListId
- JOIN Webs ON Webs.Id = AllLists.tp_WebId
- WHERE
- AllDocs.Extension = ‘master’
- ORDER BY
- Webs.Title
Output
Returns top 100 documents that are versioned and is based on doc size
Query
- SELECT
- TOP 100 Webs.FullUrl As SiteUrl,
- Webs.Title ‘Document / List Library Title’,
- DirName + ‘ / ’ + LeafName AS ‘Document Name’,
- COUNT(AllDocversions.UIVersion) AS ‘Total Version’,
- SUM(
- CAST(
- (
- CAST(
- CAST(
- AllDocversions.Size as decimal(10, 2)
- )/ 1024 As decimal(10, 2)
- )/ 1024
- ) AS Decimal(10, 2)
- )
- ) AS ‘Total Document Size (MB) ’,
- CAST(
- (
- CAST(
- CAST(
- AVG(AllDocversions.Size) as decimal(10, 2)
- )/ 1024 As decimal(10, 2)
- )/ 1024
- ) AS Decimal(10, 2)
- ) AS ‘Avg Document Size (MB) ’
- FROM
- Docs
- INNER JOIN AllDocversions ON Docs.Id = AllDocversions.Id
- INNER JOIN Webs On Docs.WebId = Webs.Id
- INNER JOIN Sites ON Webs.SiteId = SItes.Id
- WHERE
- Docs.Type <> 1
- AND (LeafName NOT LIKE ‘ %.stp’)
- AND (LeafName NOT LIKE ‘ %.aspx’)
- AND (LeafName NOT LIKE ‘ %.xfp’)
- AND (LeafName NOT LIKE ‘ %.dwp’)
- AND (
- LeafName NOT LIKE ‘ % template % ’
- )
- AND (LeafName NOT LIKE ‘ %.inf’)
- AND (LeafName NOT LIKE ‘ %.css’)
- GROUP BY
- Webs.FullUrl,
- Webs.Title,
- DirName + ‘ / ’ + LeafName
- ORDER BY
- ‘Total Version’ desc,
- ‘Total Document Size (MB) ’ desc
Output
Returns the document List Name, File Name, URL and the content (Binary format)
Query
- SELECT
- AllLists.tp_Title AS “List Name”,
- AllDocs.LeafName AS “File Name”,
- AllDocs.DirName AS “URL”,
- DocStreams.Content AS “Document Contnt (Binary) ”
- FROM
- AllDocs
- JOIN DocStreams ON AllDocs.Id = DocStreams.DocId
- JOIN AllLists ON AllLists.tp_id = AllDocs.ListId
Output
Returns documents by age
Query
- SELECT
- Webs.FullUrl AS SiteUrl,
- Webs.Title AS [Title],
- DirName + ‘ / ’ + LeafName AS [Document Name],
- Docs.TimeCreated
- FROM
- Docs
- INNER JOIN Webs On Docs.WebId = Webs.Id
- INNER JOIN Sites ON Webs.SiteId = Sites.Id
- WHERE
- Docs.Type <> 1
- AND (LeafName IS NOT NULL)
- AND (LeafName <> ”)
- AND (LeafName NOT LIKE ‘ %.stp’)
- AND (LeafName NOT LIKE ‘ %.aspx’)
- AND (LeafName NOT LIKE ‘ %.xfp’)
- AND (LeafName NOT LIKE ‘ %.dwp’)
- AND (
- LeafName NOT LIKE ‘ % template % ’
- )
- AND (LeafName NOT LIKE ‘ %.inf’)
- AND (LeafName NOT LIKE ‘ %.css’)
- ORDER BY
- Docs.TimeCreated DESC
Output
Returns total # of documents
Query
- SELECT
- COUNT(*) As “Total Number of Documents”
- FROM
- Docs
- INNER JOIN Webs On Docs.WebId = Webs.Id
- INNER JOIN Sites ON Webs.SiteId = Sites.Id
- WHERE
- Docs.Type <> 1
- AND (LeafName IS NOT NULL)
- AND (LeafName <> ”)
- AND (LeafName NOT LIKE ‘ %.stp’)
- AND (LeafName NOT LIKE ‘ %.aspx’)
- AND (LeafName NOT LIKE ‘ %.xfp’)
- AND (LeafName NOT LIKE ‘ %.dwp’)
- AND (
- LeafName NOT LIKE ‘ % template % ’
- )
- AND (LeafName NOT LIKE ‘ %.inf’)
- AND (LeafName NOT LIKE ‘ %.css’)
Output
Returns total size of all content
Query
- SELECT
- SUM(
- CAST(
- (
- CAST(
- CAST(
- Size AS DECIMAL(10, 2)
- )/ 1024 AS DECIMAL(10, 2)
- )/ 1024
- ) AS DECIMAL(10, 2)
- )
- ) AS “Total Size in MB”
- FROM
- Docs
- INNER JOIN Webs ON Docs.WebId = Webs.Id
- INNER JOIN Sites ON Webs.SiteId = Sites.Id
- WHERE
- Docs.Type <> 1
- AND (LeafName IS NOT NULL)
- AND (LeafName NOT LIKE ‘ %.stp’)
- AND (LeafName NOT LIKE ‘ %.aspx’)
- AND (LeafName NOT LIKE ‘ %.xfp’)
- AND (LeafName NOT LIKE ‘ %.dwp’)
- AND (
- LeafName NOT LIKE ‘ % template % ’
- )
- AND (LeafName NOT LIKE ‘ %.inf’)
- AND (LeafName NOT LIKE ‘ %.css’)
Output
In the upcoming articles of this series, I will try to add some useful queries that can retrieve some of the other pieces of vital information out of SharePoint.
Hope you found it helpful.