function test_sharepoint_listing
Tests the SharePoint document listing functionality by connecting to a SharePoint site and retrieving all documents from a specified path.
/tf/active/vicechatdev/SPFCsync/test_connections.py
63 - 89
simple
Purpose
This function serves as a diagnostic/testing utility to verify that the SharePoint integration is working correctly. It authenticates with SharePoint using Azure credentials, retrieves all documents from a configured path, and displays summary information including document count and sample document names with modification dates. Returns True on success, False on failure.
Source Code
def test_sharepoint_listing():
"""Test SharePoint document listing."""
print("Testing SharePoint document listing...")
try:
from sharepoint_client import SharePointClient
from config import Config
client = SharePointClient(
Config.SHAREPOINT_SITE_URL,
Config.AZURE_CLIENT_ID,
Config.AZURE_CLIENT_SECRET
)
documents = client.get_all_documents(Config.SHAREPOINT_DOCUMENTS_PATH)
print(f"✓ Found {len(documents)} documents in SharePoint")
if documents:
print("Sample documents:")
for doc in documents[:3]: # Show first 3
print(f" - {doc['name']} (modified: {doc['modified']})")
if len(documents) > 3:
print(f" ... and {len(documents) - 3} more")
return True
except Exception as e:
print(f"✗ SharePoint listing failed: {e}")
return False
Return Value
Returns a boolean value: True if the SharePoint document listing operation succeeds (documents are retrieved without errors), False if any exception occurs during the process. The function also prints diagnostic information to stdout including success/failure messages and document details.
Dependencies
sharepoint_clientconfig
Required Imports
from sharepoint_client import SharePointClient
from config import Config
Conditional/Optional Imports
These imports are only needed under specific conditions:
from sharepoint_client import SharePointClient
Condition: imported inside try block at runtime when function executes
Required (conditional)from config import Config
Condition: imported inside try block at runtime when function executes
Required (conditional)Usage Example
# Ensure config.py exists with 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'
# Config.SHAREPOINT_DOCUMENTS_PATH = '/Shared Documents'
# Run the test
result = test_sharepoint_listing()
if result:
print('SharePoint connection test passed')
else:
print('SharePoint connection test failed')
Best Practices
- This function is intended for testing/diagnostic purposes, not production use
- Ensure all required configuration values are set in Config before calling
- The function catches all exceptions broadly - consider more specific error handling for production code
- Prints output directly to stdout - redirect or capture output if needed in automated testing
- Only displays first 3 documents to avoid cluttering output with large document lists
- Requires proper Azure AD app registration with SharePoint permissions
- The SharePointClient and Config modules must be available in the Python path
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function test_sharepoint_connection 78.6% similar
-
function test_rest_client 76.4% similar
-
function test_sharepoint_api_call 74.5% similar
-
function test_folder_structure 72.6% similar
-
function test_sharepoint_with_token 72.5% similar