๐ Recipe ยท SharePoint & OneDrive
List All SharePoint Sites and Their Document Libraries via Microsoft Graph
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
Context
Why This Matters
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
After running this recipe you will have:
- 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 & Considerations
Things to watch for
- 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 โ this is the workaround baked into the scripts below. - 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. The script includes sequential processing and retry-on-429 handling.
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 recipe to modify sites or libraries โ NOT required for read-only inventory. |
The fastest way to get this done โ just ask Dex. Copy the prompt below and paste it into your Dex conversation.
For IT Admins
Paste into Dex CoAdmin