🔍 Code Extractor

function check_site_vs_channels

Maturity: 49

Diagnostic function that analyzes and compares SharePoint site structure, specifically examining the main site document library versus Teams channel document libraries to identify the correct library for synchronization.

File:
/tf/active/vicechatdev/SPFCsync/check_site_library.py
Lines:
10 - 167
Complexity:
moderate

Purpose

This function performs a comprehensive analysis of a SharePoint site's drive structure to help identify and differentiate between the main site document library and Teams channel libraries. It's designed as a diagnostic tool to troubleshoot synchronization issues by listing all drives, examining their contents, and highlighting expected folder structures. The function prints detailed information about each drive including IDs, types, URLs, and contents, with special markers for expected folders like '01 UCJ Research', '02 Toxicology', '03 CMC', and '04 Quality'.

Source Code

def check_site_vs_channels():
    """Check main site library vs channels document libraries"""
    config = Config()
    
    try:
        client = SharePointGraphClient(
            site_url=config.SHAREPOINT_SITE_URL,
            client_id=config.AZURE_CLIENT_ID,
            client_secret=config.AZURE_CLIENT_SECRET
        )
        
        print("✅ SharePoint Graph client initialized")
        print(f"Site: {config.SHAREPOINT_SITE_URL}")
        print()
        
    except Exception as e:
        print(f"❌ Failed to initialize client: {e}")
        return
    
    headers = {
        'Authorization': f'Bearer {client.access_token}',
        'Accept': 'application/json'
    }
    
    print("🔍 ANALYZING SITE STRUCTURE")
    print("=" * 50)
    
    # 1. Check main site drives
    print("1. MAIN SITE DRIVES:")
    print("-" * 30)
    
    try:
        drives_url = f"https://graph.microsoft.com/v1.0/sites/{client.site_id}/drives"
        response = requests.get(drives_url, headers=headers)
        
        if response.status_code == 200:
            drives_data = response.json()
            
            for drive in drives_data.get('value', []):
                drive_name = drive.get('name', 'Unknown')
                drive_id = drive.get('id', 'Unknown')
                web_url = drive.get('webUrl', 'No URL')
                drive_type = drive.get('driveType', 'unknown')
                
                print(f"📁 {drive_name}")
                print(f"   ID: {drive_id}")
                print(f"   Type: {drive_type}")
                print(f"   URL: {web_url}")
                
                # Check if this looks like the main site library
                if 'Shared%20Documents' in web_url and 'channel' not in web_url.lower():
                    print("   đŸŽ¯ This appears to be the MAIN SITE LIBRARY")
                    
                    # Get contents
                    root_url = f"https://graph.microsoft.com/v1.0/drives/{drive_id}/root/children"
                    try:
                        root_response = requests.get(root_url, headers=headers)
                        if root_response.status_code == 200:
                            root_data = root_response.json()
                            if 'value' in root_data:
                                folders = [item for item in root_data['value'] if 'folder' in item]
                                files = [item for item in root_data['value'] if 'file' in item]
                                
                                print(f"   📁 {len(folders)} folders, 📄 {len(files)} files")
                                
                                # List folder names - look for the expected ones
                                if folders:
                                    print("   Folders found:")
                                    expected_folders = ['01 UCJ Research', '02 Toxicology', '03 CMC', '04 Quality']
                                    
                                    for folder in folders:
                                        folder_name = folder.get('name', 'Unknown')
                                        is_expected = any(exp in folder_name for exp in expected_folders)
                                        marker = "đŸŽ¯" if is_expected else "📁"
                                        print(f"     {marker} {folder_name}")
                                        
                                        if is_expected:
                                            print(f"         ✅ This matches expected folder pattern!")
                        else:
                            print(f"   ❌ Failed to get contents: {root_response.status_code}")
                    except Exception as e:
                        print(f"   ❌ Error getting contents: {e}")
                
                elif 'channel' in web_url.lower() or '00Strategy' in web_url:
                    print("   đŸ“ĸ This appears to be a TEAMS CHANNEL library")
                
                print()
                
    except Exception as e:
        print(f"❌ Error checking drives: {e}")
    
    # 2. Try to find the default documents library specifically
    print("\n2. TRYING TO ACCESS DEFAULT DOCUMENTS LIBRARY:")
    print("-" * 50)
    
    try:
        # Try the standard site documents library endpoint
        default_drive_url = f"https://graph.microsoft.com/v1.0/sites/{client.site_id}/drive"
        response = requests.get(default_drive_url, headers=headers)
        
        if response.status_code == 200:
            default_drive = response.json()
            drive_name = default_drive.get('name', 'Unknown')
            drive_id = default_drive.get('id', 'Unknown')
            web_url = default_drive.get('webUrl', 'No URL')
            
            print(f"📁 Default Drive: {drive_name}")
            print(f"   ID: {drive_id}")
            print(f"   URL: {web_url}")
            
            # This should be the main site library
            if drive_id != client.drive_id:
                print("   đŸŽ¯ This is DIFFERENT from our current drive!")
                print("   đŸŽ¯ This is likely the MAIN SITE LIBRARY we want to sync!")
                
                # Get contents
                root_url = f"https://graph.microsoft.com/v1.0/drives/{drive_id}/root/children"
                try:
                    root_response = requests.get(root_url, headers=headers)
                    if root_response.status_code == 200:
                        root_data = root_response.json()
                        if 'value' in root_data:
                            folders = [item for item in root_data['value'] if 'folder' in item]
                            files = [item for item in root_data['value'] if 'file' in item]
                            
                            print(f"   📁 {len(folders)} folders, 📄 {len(files)} files")
                            
                            # List folder names - look for the expected ones
                            if folders:
                                print("   Folders found:")
                                expected_folders = ['01 UCJ Research', '02 Toxicology', '03 CMC', '04 Quality']
                                
                                for folder in folders:
                                    folder_name = folder.get('name', 'Unknown')
                                    is_expected = any(exp in folder_name for exp in expected_folders)
                                    marker = "đŸŽ¯" if is_expected else "📁"
                                    print(f"     {marker} {folder_name}")
                                    
                                    if is_expected:
                                        print(f"         ✅ FOUND EXPECTED FOLDER!")
                            
                            # Show some files too
                            if files:
                                print("   Some files:")
                                for file in files[:5]:
                                    file_name = file.get('name', 'Unknown')
                                    print(f"     📄 {file_name}")
                    else:
                        print(f"   ❌ Failed to get contents: {root_response.status_code}")
                except Exception as e:
                    print(f"   ❌ Error getting contents: {e}")
            else:
                print("   â„šī¸  This is the same drive we're already using")
        else:
            print(f"❌ Failed to get default drive: {response.status_code}")
            
    except Exception as e:
        print(f"❌ Error checking default drive: {e}")

Return Value

This function does not return any value (implicitly returns None). All output is printed to stdout, including drive information, folder structures, file listings, and diagnostic messages with emoji markers for visual clarity.

Dependencies

  • requests
  • sharepoint_graph_client
  • config

Required Imports

import requests
from sharepoint_graph_client import SharePointGraphClient
from config import Config

Usage Example

# Ensure config.py has required settings:
# SHAREPOINT_SITE_URL = 'https://yourtenant.sharepoint.com/sites/yoursite'
# AZURE_CLIENT_ID = 'your-client-id'
# AZURE_CLIENT_SECRET = 'your-client-secret'

from your_module import check_site_vs_channels

# Run the diagnostic check
check_site_vs_channels()

# Output will be printed to console showing:
# - All drives in the SharePoint site
# - Main site library identification
# - Teams channel libraries
# - Folder and file listings
# - Comparison with expected folder structure

Best Practices

  • This is a diagnostic/debugging function meant for development and troubleshooting, not production use
  • Ensure proper Azure AD application permissions are configured before running
  • The function expects specific folder naming patterns ('01 UCJ Research', '02 Toxicology', etc.) - modify the expected_folders list if your structure differs
  • Review console output carefully to identify the correct drive_id for your main site library
  • The function makes multiple API calls to Microsoft Graph - be mindful of rate limits
  • Error handling is built-in but failures are printed rather than raised, so monitor console output
  • The function uses emoji markers (✅, ❌, đŸŽ¯, 📁, etc.) for visual clarity - ensure your console supports Unicode
  • Access tokens are obtained through SharePointGraphClient - ensure the client is properly authenticated
  • The function distinguishes between main site libraries and Teams channel libraries by examining URLs for 'channel' keywords

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function access_main_site_library 71.1% similar

    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.

    From: /tf/active/vicechatdev/SPFCsync/find_main_library.py
  • function explore_site_structure 68.7% 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 quick_test 67.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 67.4% 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_v35 66.8% similar

    A diagnostic function that explores SharePoint site structure to investigate why only 2 folders are visible when more are expected in the web interface.

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