function explore_drive_root
Retrieves and displays the contents of a SharePoint drive's root directory, showing folders and files with their counts and names.
/tf/active/vicechatdev/SPFCsync/diagnostic_comprehensive.py
89 - 112
simple
Purpose
This function queries the Microsoft Graph API to explore the root level of a specific SharePoint drive. It fetches all items in the root directory, categorizes them into folders and files, and prints a summary along with the first 10 folder names. This is useful for inspecting SharePoint drive contents, debugging drive access, or providing users with a quick overview of available resources in a drive.
Source Code
def explore_drive_root(site_id, drive_id, headers, drive_name):
"""Explore the root of a specific drive."""
try:
root_url = f"https://graph.microsoft.com/v1.0/sites/{site_id}/drives/{drive_id}/root/children"
response = requests.get(root_url, headers=headers)
if response.status_code == 200:
items_data = response.json()
folders = [item for item in items_data.get('value', []) if 'folder' in item]
files = [item for item in items_data.get('value', []) if 'file' in item]
print(f" 📁 {len(folders)} folders, 📄 {len(files)} files in '{drive_name}'")
if folders:
print(" Folders:")
for folder in folders[:10]: # Show first 10 folders
print(f" 📁 {folder.get('name', 'Unknown')}")
if len(folders) > 10:
print(f" ... and {len(folders) - 10} more folders")
else:
print(f" Failed to get root items: {response.status_code}")
except Exception as e:
print(f" Error exploring drive root: {e}")
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
site_id |
- | - | positional_or_keyword |
drive_id |
- | - | positional_or_keyword |
headers |
- | - | positional_or_keyword |
drive_name |
- | - | positional_or_keyword |
Parameter Details
site_id: The unique identifier (GUID) of the SharePoint site that contains the drive. This is obtained from Microsoft Graph API and typically looks like a UUID format string.
drive_id: The unique identifier (GUID) of the specific drive within the SharePoint site to explore. Each SharePoint site can have multiple drives (document libraries).
headers: A 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>'}).
drive_name: A human-readable string name of the drive, used only for display purposes in the console output to help users identify which drive is being explored.
Return Value
This function returns None (implicitly). It performs side effects by printing information to the console, including folder/file counts, folder names, and any error messages encountered during execution.
Dependencies
requests
Required Imports
import requests
Usage Example
import requests
# Assume you have obtained an access token through OAuth2 flow
access_token = 'eyJ0eXAiOiJKV1QiLCJhbGc...'
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
# SharePoint site and drive identifiers
site_id = 'contoso.sharepoint.com,12345678-1234-1234-1234-123456789012,87654321-4321-4321-4321-210987654321'
drive_id = 'b!abcdefghijklmnopqrstuvwxyz1234567890'
drive_name = 'Documents'
# Explore the drive root
explore_drive_root(site_id, drive_id, headers, drive_name)
# Expected output:
# 📁 5 folders, 📄 3 files in 'Documents'
# Folders:
# 📁 Projects
# 📁 Reports
# 📁 Templates
# 📁 Archive
# 📁 Shared
Best Practices
- Ensure the access token in headers is valid and not expired before calling this function
- Handle authentication and token refresh logic separately before calling this function
- The function only displays the first 10 folders to avoid console clutter; consider modifying the limit for different use cases
- Error handling is basic (prints to console); consider wrapping calls in try-except blocks for production use
- The function makes synchronous HTTP requests; for multiple drives, consider implementing async/await or threading for better performance
- Verify that the user/application has appropriate permissions (Sites.Read.All minimum) before calling
- Consider implementing pagination handling if drives contain more than the default page size of items returned by Graph API
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function explore_all_drives 87.0% similar
-
function get_root_folders 81.2% similar
-
function explore_site_structure 74.3% similar
-
function test_site_drive_access 72.6% similar
-
function test_folder_structure 71.9% similar