šŸ” Code Extractor

function compare_with_expected_folders

Maturity: 45

Compares SharePoint folders found via Microsoft Graph API against a predefined list of expected folder names from a reference screenshot, reporting matches, missing folders, and additional folders.

File:
/tf/active/vicechatdev/SPFCsync/test_folder_structure.py
Lines:
144 - 213
Complexity:
moderate

Purpose

This function validates SharePoint folder structure by comparing actual folders retrieved from a SharePoint site against an expected list. It's useful for testing, auditing, or verifying that a SharePoint site has the correct folder organization. The function provides detailed output showing which folders match expectations, which are missing, and which are additional/unexpected.

Source Code

def compare_with_expected_folders():
    """Compare found folders with what we expect from the screenshot."""
    expected_folders = [
        "00 Strategy",
        "01 UCJ Research", 
        "02 Toxicology",
        "03 CMC",
        "04 Quality",
        "05 Clinical",
        "06 Regulatory",
        "07 Electronic Data Room",
        "08 IP",
        "09 Compliance",
        "1 - 1 ViceBio Management Team meeting",
        "1 - 2 R&V Development Project Team meeting",
        "1 - 3 Development Project Team meeting",
        "1 - 4 Board meetings",
        "10 Program Management",
        "11 DEVELOPMENT DOCUMENTATION Management",
        "13 - Source Documents - All functions"
    ]
    
    print("\nšŸ” COMPARISON WITH EXPECTED FOLDERS:")
    print("-" * 45)
    
    try:
        from sharepoint_graph_client import SharePointGraphClient
        from config import Config
        
        client = SharePointGraphClient(
            Config.SHAREPOINT_SITE_URL,
            Config.AZURE_CLIENT_ID,
            Config.AZURE_CLIENT_SECRET
        )
        
        found_folders = get_root_folders(client)
        found_names = [f['name'] for f in found_folders]
        
        print("āœ… FOUND FOLDERS:")
        for name in sorted(found_names):
            print(f"   šŸ“ {name}")
        
        print(f"\nšŸ“Š Found {len(found_names)} folders")
        print(f"šŸ“Š Expected ~{len(expected_folders)} folders from screenshot")
        
        # Check for matches
        matches = set(found_names) & set(expected_folders)
        missing = set(expected_folders) - set(found_names)
        extra = set(found_names) - set(expected_folders)
        
        if matches:
            print(f"\nāœ… MATCHING FOLDERS ({len(matches)}):")
            for name in sorted(matches):
                print(f"   šŸ“ {name}")
        
        if missing:
            print(f"\nāš ļø  EXPECTED BUT NOT FOUND ({len(missing)}):")
            for name in sorted(missing):
                print(f"   šŸ“ {name}")
        
        if extra:
            print(f"\nšŸ†• ADDITIONAL FOLDERS FOUND ({len(extra)}):")
            for name in sorted(extra):
                print(f"   šŸ“ {name}")
        
        return len(found_names) > 0
        
    except Exception as e:
        print(f"āŒ Comparison failed: {e}")
        return False

Return Value

Returns a boolean value: True if at least one folder was found in the SharePoint site (indicating successful connection and retrieval), False if the comparison failed due to an exception or no folders were found.

Dependencies

  • sharepoint_graph_client
  • config
  • requests

Required Imports

from sharepoint_graph_client import SharePointGraphClient
from config import Config

Conditional/Optional Imports

These imports are only needed under specific conditions:

from sharepoint_graph_client import SharePointGraphClient

Condition: imported inside try block when executing the comparison logic

Required (conditional)
from config import Config

Condition: imported inside try block when executing the comparison logic

Required (conditional)

Usage Example

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

# Ensure get_root_folders function is defined:
def get_root_folders(client):
    # Implementation to retrieve folders
    return client.list_folders('/')

# Call the comparison function
success = compare_with_expected_folders()

if success:
    print('Comparison completed successfully')
else:
    print('Comparison failed or no folders found')

Best Practices

  • Ensure the Config class has all required SharePoint and Azure AD credentials configured before calling this function
  • The function depends on a 'get_root_folders' function being available in the same scope - ensure this dependency is satisfied
  • The expected_folders list is hardcoded and should be updated if the reference screenshot or expected structure changes
  • This function prints output directly to console - consider capturing stdout if you need to process the output programmatically
  • The function catches all exceptions broadly - consider more specific exception handling for production use
  • The SharePointGraphClient must be properly implemented with authentication handling for Microsoft Graph API
  • Ensure proper Azure AD app permissions are configured (Sites.Read.All or similar) for SharePoint access

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function search_for_folders 73.5% similar

    Searches for specific predefined folders in a SharePoint site using Microsoft Graph API and prints the search results with their locations.

    From: /tf/active/vicechatdev/SPFCsync/diagnostic_comprehensive.py
  • function test_folder_structure 73.1% similar

    Tests SharePoint folder structure by listing root-level folders, displaying their contents, and providing a summary of total folders and documents.

    From: /tf/active/vicechatdev/SPFCsync/test_folder_structure.py
  • function main_v21 72.9% similar

    Main test function that validates SharePoint folder structure connectivity and configuration, comparing actual folders against expected structure.

    From: /tf/active/vicechatdev/SPFCsync/test_folder_structure.py
  • function analyze_structure 72.7% similar

    Analyzes and reports on the folder structure of a SharePoint site, displaying folder paths, file counts, and searching for expected folder patterns.

    From: /tf/active/vicechatdev/SPFCsync/analyze_structure.py
  • function explore_alternative_endpoints 70.9% similar

    Tests multiple Microsoft Graph API endpoints to locate missing folders in a SharePoint drive by trying different URL patterns and searching for expected folders.

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