🔍 Code Extractor

function access_main_site_library

Maturity: 52

Authenticates with Microsoft Graph API and attempts to locate and access the main SharePoint site library using multiple discovery approaches, displaying detailed information about sites, drives, and folder structures.

File:
/tf/active/vicechatdev/SPFCsync/find_main_library.py
Lines:
9 - 164
Complexity:
complex

Purpose

This diagnostic function is designed to troubleshoot and discover SharePoint site libraries by trying multiple Microsoft Graph API endpoints. It authenticates using OAuth2 client credentials, searches for a specific SharePoint site (vicebio.com), enumerates its document libraries, and identifies the main Documents library by checking for expected folder patterns. The function provides verbose console output to help debug SharePoint access issues and identify the correct drive ID for document operations.

Source Code

def access_main_site_library():
    """Try to access the main site library directly"""
    config = Config()
    
    # Get access token
    tenant = config.SHAREPOINT_SITE_URL.split('.sharepoint.com')[0].split('https://')[-1]
    token_url = f"https://login.microsoftonline.com/{tenant}.onmicrosoft.com/oauth2/v2.0/token"
    
    token_data = {
        'client_id': config.AZURE_CLIENT_ID,
        'client_secret': config.AZURE_CLIENT_SECRET,
        'scope': 'https://graph.microsoft.com/.default',
        'grant_type': 'client_credentials'
    }
    
    try:
        token_response = requests.post(token_url, data=token_data)
        if token_response.status_code != 200:
            print(f"❌ Failed to get token: {token_response.status_code}")
            return
        
        access_token = token_response.json().get('access_token')
        if not access_token:
            print("❌ No access token received")
            return
            
        print("✅ Got access token")
        
    except Exception as e:
        print(f"❌ Token error: {e}")
        return
    
    headers = {
        'Authorization': f'Bearer {access_token}',
        'Accept': 'application/json'
    }
    
    print("🔍 DIRECT ACCESS TO MAIN SITE LIBRARY")
    print("=" * 50)
    
    # Try different approaches to get the main site
    site_approaches = [
        # Approach 1: Direct site by URL
        f"https://graph.microsoft.com/v1.0/sites/netorgft5138176.sharepoint.com:/sites/vicebio.com",
        
        # Approach 2: Search for the site
        "https://graph.microsoft.com/v1.0/sites?search=vicebio.com",
        
        # Approach 3: Get site by hostname and path
        "https://graph.microsoft.com/v1.0/sites/netorgft5138176.sharepoint.com,80f122f0-6aba-4539-ae6c-8103516134c4,57142f54-8c26-41b5-81f8-bf459b25f566"
    ]
    
    for i, approach_url in enumerate(site_approaches, 1):
        print(f"\n{i}. Trying: {approach_url}")
        print("-" * 60)
        
        try:
            response = requests.get(approach_url, headers=headers)
            
            if response.status_code == 200:
                result = response.json()
                
                if 'value' in result:  # Search results
                    print(f"✅ Found {len(result['value'])} sites:")
                    for site in result['value']:
                        site_name = site.get('displayName', 'Unknown')
                        site_url = site.get('webUrl', 'No URL')
                        site_id = site.get('id', 'No ID')
                        print(f"   📍 {site_name}")
                        print(f"      URL: {site_url}")
                        print(f"      ID: {site_id}")
                        
                        # If this looks like our main site, try to get its drives
                        if 'vicebio.com' in site_url and '/sites/' in site_url:
                            print(f"      🎯 This looks like our main site!")
                            
                            # Get drives for this site
                            drives_url = f"https://graph.microsoft.com/v1.0/sites/{site_id}/drives"
                            drives_response = requests.get(drives_url, headers=headers)
                            
                            if drives_response.status_code == 200:
                                drives_data = drives_response.json()
                                print(f"      📁 Found {len(drives_data.get('value', []))} drives:")
                                
                                for drive in drives_data.get('value', []):
                                    drive_name = drive.get('name', 'Unknown')
                                    drive_id = drive.get('id', 'Unknown')
                                    drive_url = drive.get('webUrl', 'No URL')
                                    
                                    print(f"         📁 {drive_name}")
                                    print(f"            URL: {drive_url}")
                                    
                                    # Check if this is the main Documents library
                                    if drive_name == 'Documents' and 'channel' not in drive_url.lower():
                                        print(f"            🎯 MAIN DOCUMENTS LIBRARY FOUND!")
                                        
                                        # Get root contents
                                        root_url = f"https://graph.microsoft.com/v1.0/drives/{drive_id}/root/children"
                                        root_response = requests.get(root_url, headers=headers)
                                        
                                        if root_response.status_code == 200:
                                            root_data = root_response.json()
                                            folders = [item for item in root_data.get('value', []) if 'folder' in item]
                                            files = [item for item in root_data.get('value', []) if 'file' in item]
                                            
                                            print(f"            📁 {len(folders)} folders, 📄 {len(files)} files")
                                            
                                            expected_patterns = ['01 UCJ', '02 Toxicology', '03 CMC', '04 Quality']
                                            found_expected = False
                                            
                                            print(f"            Folders:")
                                            for folder in folders:
                                                folder_name = folder.get('name', 'Unknown')
                                                is_expected = any(pattern in folder_name for pattern in expected_patterns)
                                                marker = "🎯" if is_expected else "📁"
                                                print(f"              {marker} {folder_name}")
                                                if is_expected:
                                                    found_expected = True
                                                    
                                            if found_expected:
                                                print(f"            ✅ FOUND EXPECTED FOLDERS! This is the right library!")
                                                print(f"            📝 Drive ID to use: {drive_id}")
                                        else:
                                            print(f"            ❌ Failed to get root contents: {root_response.status_code}")
                            else:
                                print(f"      ❌ Failed to get drives: {drives_response.status_code}")
                        print()
                
                else:  # Direct site result
                    site_name = result.get('displayName', 'Unknown')
                    site_url = result.get('webUrl', 'No URL')
                    site_id = result.get('id', 'No ID')
                    print(f"✅ Site: {site_name}")
                    print(f"   URL: {site_url}")
                    print(f"   ID: {site_id}")
                    
                    # Get drives for this site
                    drives_url = f"https://graph.microsoft.com/v1.0/sites/{site_id}/drives"
                    drives_response = requests.get(drives_url, headers=headers)
                    
                    if drives_response.status_code == 200:
                        drives_data = drives_response.json()
                        print(f"   📁 Found {len(drives_data.get('value', []))} drives")
                    else:
                        print(f"   ❌ Failed to get drives: {drives_response.status_code}")
            else:
                print(f"❌ Failed: {response.status_code}")
                if response.content:
                    try:
                        error_data = response.json()
                        print(f"   Error: {error_data}")
                    except:
                        print(f"   Raw error: {response.text[:200]}")
                
        except Exception as e:
            print(f"❌ Exception: {e}")

Return Value

Returns None implicitly. The function does not return any value but prints extensive diagnostic information to the console, including authentication status, site discovery results, drive listings, and folder contents. Early returns occur on authentication failures.

Dependencies

  • requests
  • config

Required Imports

import requests
from config import Config

Usage Example

# Ensure config.py exists with required settings:
# class Config:
#     SHAREPOINT_SITE_URL = 'https://netorgft5138176.sharepoint.com/sites/vicebio.com'
#     AZURE_CLIENT_ID = 'your-client-id'
#     AZURE_CLIENT_SECRET = 'your-client-secret'

from your_module import access_main_site_library
import requests
from config import Config

# Run the diagnostic function
access_main_site_library()

# The function will print detailed output to console:
# - Authentication status
# - Site discovery attempts
# - Available drives and libraries
# - Folder structure of main Documents library
# - Drive ID to use for further operations

Best Practices

  • This is a diagnostic/debugging function intended for development and troubleshooting, not production use
  • Ensure Azure AD application has appropriate Graph API permissions before running
  • The function makes multiple API calls and may take time to complete
  • Review console output to identify the correct drive ID for your main Documents library
  • Client credentials flow requires application-level permissions, not delegated user permissions
  • The function is hardcoded to search for 'vicebio.com' site - modify site_approaches list for different sites
  • Expected folder patterns ('01 UCJ', '02 Toxicology', etc.) are specific to this use case and should be customized
  • Consider implementing proper logging instead of print statements for production scenarios
  • Handle sensitive credentials securely - avoid hardcoding client secrets
  • The function performs no error recovery and returns early on failures
  • API rate limits may apply when making multiple Graph API requests

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function explore_site_structure 77.5% 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_graph_sites_api 77.1% similar

    Tests Microsoft Graph API access to SharePoint sites by attempting to list available sites and locate a specific target site (vicebio.com), then tests drive access if found.

    From: /tf/active/vicechatdev/SPFCsync/check_tenant_config.py
  • function quick_test 75.9% similar

    A diagnostic function that tests SharePoint Graph API connectivity and verifies access to the main site library by checking for expected folder structures in the root directory.

    From: /tf/active/vicechatdev/SPFCsync/quick_test.py
  • function check_all_libraries 75.6% 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
  • function main_v44 75.5% similar

    Diagnostic function that tests SharePoint tenant configuration by checking Microsoft Graph API access and provides recommendations based on the results.

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