function explore_all_drives
Retrieves and displays all document drives from a SharePoint site using Microsoft Graph API, then explores the root folder of each drive.
/tf/active/vicechatdev/SPFCsync/diagnostic_comprehensive.py
65 - 87
moderate
Purpose
This function queries the Microsoft Graph API to fetch all drives (document libraries) associated with a specific SharePoint site. It iterates through each drive, prints metadata (name, ID, type, web URL), and calls explore_drive_root() to examine the contents of each drive's root folder. This is useful for discovering and auditing SharePoint site structure, document libraries, and their contents.
Source Code
def explore_all_drives(site_id, headers):
"""Explore all drives in the SharePoint site."""
try:
drives_url = f"https://graph.microsoft.com/v1.0/sites/{site_id}/drives"
response = requests.get(drives_url, headers=headers)
if response.status_code == 200:
drives_data = response.json()
for i, drive in enumerate(drives_data.get('value', []), 1):
print(f"{i}. Drive: {drive.get('name', 'Unknown')}")
print(f" ID: {drive.get('id', 'Unknown')}")
print(f" Type: {drive.get('driveType', 'Unknown')}")
print(f" Web URL: {drive.get('webUrl', 'Unknown')}")
# Explore this drive's root
explore_drive_root(site_id, drive.get('id'), headers, drive.get('name', 'Unknown'))
print()
else:
print(f"Failed to get drives: {response.status_code}")
except Exception as e:
print(f"Error exploring drives: {e}")
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
site_id |
- | - | positional_or_keyword |
headers |
- | - | positional_or_keyword |
Parameter Details
site_id: The unique identifier of the SharePoint site. This is a GUID-format string obtained from Microsoft Graph API (e.g., 'contoso.sharepoint.com,abc123,def456'). Required to construct the API endpoint URL.
headers: Dictionary containing HTTP headers for authentication with Microsoft Graph API. Must include an 'Authorization' header with a valid Bearer token (e.g., {'Authorization': 'Bearer <access_token>'}). Required for API authentication.
Return Value
This function does not return any value (implicitly returns None). It produces side effects by printing drive information to stdout and making additional API calls via explore_drive_root(). On success, it prints formatted drive details; on failure, it prints error messages.
Dependencies
requests
Required Imports
import requests
Usage Example
import requests
# Assume you have obtained an access token
access_token = 'your_access_token_here'
site_id = 'contoso.sharepoint.com,abc-123-def,ghi-456-jkl'
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
# Note: explore_drive_root() must be defined before calling this function
def explore_drive_root(site_id, drive_id, headers, drive_name):
# Implementation of drive root exploration
pass
explore_all_drives(site_id, headers)
# Output will print drive information and explore each drive's root
Best Practices
- Ensure the access token in headers is valid and not expired before calling this function
- The function depends on explore_drive_root() being defined; ensure this dependency is available
- Handle rate limiting from Microsoft Graph API by implementing retry logic in production code
- Consider adding pagination support as the API may return @odata.nextLink for sites with many drives
- Implement proper logging instead of print statements for production environments
- Add timeout parameters to requests.get() to prevent hanging on network issues
- Validate site_id format before making API calls to fail fast on invalid input
- Consider returning structured data instead of printing for better testability and reusability
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function explore_drive_root 87.0% similar
-
function explore_site_structure 81.0% similar
-
function test_site_drive_access 79.2% similar
-
function explore_document_libraries 78.3% similar
-
function check_all_libraries 77.9% similar