🔍 Code Extractor

function explore_all_drives

Maturity: 46

Retrieves and displays all document drives from a SharePoint site using Microsoft Graph API, then explores the root folder of each drive.

File:
/tf/active/vicechatdev/SPFCsync/diagnostic_comprehensive.py
Lines:
65 - 87
Complexity:
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

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function explore_drive_root 87.0% similar

    Retrieves and displays the contents of a SharePoint drive's root directory, showing folders and files with their counts and names.

    From: /tf/active/vicechatdev/SPFCsync/diagnostic_comprehensive.py
  • function explore_site_structure 81.0% similar

    Explores and displays the complete structure of a SharePoint site using Microsoft Graph API, including drives, document libraries, lists, and alternative API endpoints.

    From: /tf/active/vicechatdev/SPFCsync/diagnostic_comprehensive.py
  • function test_site_drive_access 79.2% similar

    Tests access to a SharePoint site's document library (drives) via Microsoft Graph API and attempts to access the first drive's root items.

    From: /tf/active/vicechatdev/SPFCsync/check_tenant_config.py
  • function explore_document_libraries 78.3% similar

    Retrieves and displays all document libraries from a SharePoint site using Microsoft Graph API, then explores items within each library.

    From: /tf/active/vicechatdev/SPFCsync/diagnostic_comprehensive.py
  • function check_all_libraries 77.9% similar

    Discovers and lists all document libraries in a SharePoint site using Microsoft Graph API, displaying their metadata and contents.

    From: /tf/active/vicechatdev/SPFCsync/check_libraries.py
← Back to Browse