Enumerate every SharePoint site in your M365 tenant and the document libraries (drives) attached to each, then export the inventory for governance review.
Complexity: Intermediate. Impact: read-only + governance + reporting + sharepoint.
Why use this workflow
Building a complete inventory of SharePoint sites and their document libraries is a foundational governance task. Admins need this data to:
- Track content sprawl and identify abandoned or duplicate sites.
- Map data residency before running DLP, retention, or sensitivity label rollouts.
- Feed migration, backup, and archival tooling with an accurate source-of-truth list.
- Answer audit questions like "which libraries exist under site X?" or "how many document libraries are in the tenant?"
The SharePoint admin center shows a site list, but it does not expose the per-site drive (document library) URLs in a single exportable view. Microsoft Graph's /sites and /sites/{id}/drives endpoints solve this cleanly and programmatically.
Expected outcomes
- A complete list of SharePoint sites in the tenant (display name, site ID, web URL).
- For each site, the attached document libraries (drive name, drive ID, web URL, drive type).
- A flat CSV report (
SharePoint_Sites_and_Libraries.csv) with one row per library, suitable for Excel, Power BI, or migration tooling. - A repeatable process you can schedule to detect new sites/libraries over time.
Risks and considerations
- Search indexing lag: Newly created sites can take minutes to hours to appear via
/sites?search=*. For freshly provisioned sites, query by hostname or site path directly. - The
search=*wildcard sometimes returns HTTP 500 on some tenants. Usesearch=(empty value) or omit the parameter and page through results as a fallback. - Result cap: The search endpoint returns roughly the top 500 sites. Very large tenants should enumerate via
/sites/getAllSites(beta) or via the SharePoint Admin REST API for full coverage. - System / hidden libraries: The
/drivesendpoint returns document libraries only. Hidden system libraries (Form Templates, Style Library, etc.) are excluded by design. Use/sites/{id}/lists?$filter=list/template eq 'documentLibrary'if you need the complete list. - Read-only scope is enough:
Sites.Read.Allis sufficient. Do NOT grantSites.FullControl.Allfor an inventory report. - Throttling: Tenants with hundreds of sites will hit Graph throttling if you fire drive lookups in parallel without backoff. Process sites sequentially and retry on HTTP 429.
Required permissions
| Permission | Why it's needed |
|---|---|
| Sites.Read.All | Read-only access to list all SharePoint sites and enumerate drives (document libraries). Sufficient for an inventory report. |
| Sites.ReadWrite.All | Only needed if you intend to extend this workflow to modify sites or libraries: NOT required for read-only inventory. |
Ask Dex
Paste the prompt into a Dex conversation.
Prompt for IT admins
List every SharePoint site in our tenant along with its document library URLs. Export the results as a CSV with columns SiteName, SiteUrl, LibraryName, LibraryUrl.
Manual steps (admin center)
This approach works for small tenants but does not expose library URLs directly: you must visit each site to capture them.
Step 1: Open the SharePoint admin center
- Sign in to https://admin.microsoft.com as a Global Administrator or SharePoint Administrator.
- In the left nav, expand Admin centers > SharePoint.
Step 2: Export the site list
- In the SharePoint admin center, go to Sites > Active sites.
- Click Export (top-right toolbar) to download a CSV of all active sites. This gives you site name, URL, template, storage, and owner: but NOT library URLs.
Step 3: Capture document libraries for each site
- For each site in the exported CSV, click the site URL to open it.
- In the site's left navigation, note each item listed under Documents or the Site contents page (gear icon > Site contents).
- Each document library's URL is of the form
https://<tenant>.sharepoint.com/sites/<site>/<LibraryName>. Copy these into your spreadsheet.
Recommendation: For anything beyond ~5 sites, skip this manual path and use the API or PowerShell workflows below.
Microsoft Graph API
Two endpoints do the heavy lifting: list sites, then for each site list its drives.
Step 1: List all SharePoint sites
GET https://graph.microsoft.com/v1.0/sites?search=&$select=id,displayName,webUrl&$top=999Note: use search= (empty) rather than search=*; the wildcard form returns HTTP 500 on some tenants.
Sample response:
{
"value": [
{
"id": "contoso.sharepoint.com,6bee9a7d-...,348121bc-...",
"displayName": "Mark 8 Project Team",
"webUrl": "https://contoso.sharepoint.com/sites/Mark8ProjectTeam"
}
]
}Step 2: List document libraries for each site
GET https://graph.microsoft.com/v1.0/sites/{site-id}/drives?$select=id,name,webUrl,driveTypeSample response:
{
"value": [
{
"id": "b!fZru...",
"name": "Documents",
"webUrl": "https://contoso.sharepoint.com/sites/Mark8ProjectTeam/Shared%20Documents",
"driveType": "documentLibrary"
}
]
}Step 3: Handle pagination
If either response contains an @odata.nextLink, follow it until absent. Large tenants will paginate the site list into multiple pages of up to 200-500 items.
Step 4: Watch for 429 throttling
When fanning out drive lookups across many sites, respect Retry-After headers. A 200ms delay between sequential calls keeps most tenants safely under the limit.
PowerShell
Prerequisites
- Microsoft.Graph PowerShell SDK:
Install-Module Microsoft.Graph -Scope CurrentUser Sites.Read.Allconsent granted (delegated or application).
How it works
- Connects to Graph with the
Sites.Read.Allscope. - Calls
Get-MgSite -Search ""and pages through all results. - For each site, calls
Get-MgSiteDriveto list document libraries. - Flattens the data into one row per library and exports to CSV.
- Includes retry logic for HTTP 429 (throttling) and skips sites that return permission errors.
Run it
.\Get-SharePointSitesAndLibraries.ps1 -OutputPath "C:\Reports\SPInventory.csv"The script prints a per-site progress line and a final summary (total sites, total libraries, sites with errors).