function get_folder_documents
Retrieves and filters documents from a SharePoint folder by matching the folder name in document paths.
/tf/active/vicechatdev/SPFCsync/test_folder_structure.py
125 - 142
simple
Purpose
This function queries a SharePoint client to get all documents from the root directory and filters them to return only those belonging to a specific folder. It handles two path patterns: documents directly under the folder (folder_name/) and documents with the folder name in their path at the first level of hierarchy.
Source Code
def get_folder_documents(client, folder_name):
"""Get documents from a specific folder."""
try:
all_documents = client.get_all_documents("/")
# Filter documents that belong to this folder
folder_documents = []
for doc in all_documents:
# Check if the document's path starts with the folder name
if doc['relative_path'].startswith(f"{folder_name}/") or \
(folder_name in doc['relative_path'] and doc['relative_path'].count('/') == 1):
folder_documents.append(doc)
return folder_documents
except Exception as e:
print(f"Error getting documents for folder {folder_name}: {e}")
return []
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
client |
- | - | positional_or_keyword |
folder_name |
- | - | positional_or_keyword |
Parameter Details
client: An instance of SharePointGraphClient that provides the get_all_documents() method to retrieve documents from SharePoint. Must be authenticated and configured to access the target SharePoint site.
folder_name: String representing the name of the folder to filter documents from. Should match the folder name as it appears in the document's relative_path field. Case-sensitive.
Return Value
Returns a list of document dictionaries that belong to the specified folder. Each document dictionary contains at least a 'relative_path' key. Returns an empty list if no documents are found, if an error occurs, or if the folder doesn't exist. The function catches all exceptions and returns an empty list rather than raising errors.
Dependencies
sharepoint_graph_clientconfig
Required Imports
from sharepoint_graph_client import SharePointGraphClient
from config import Config
Usage Example
from sharepoint_graph_client import SharePointGraphClient
from config import Config
# Initialize the SharePoint client
config = Config()
client = SharePointGraphClient(config)
# Get documents from a specific folder
folder_name = "Reports"
documents = get_folder_documents(client, folder_name)
# Process the retrieved documents
for doc in documents:
print(f"Document path: {doc['relative_path']}")
Best Practices
- Ensure the SharePoint client is properly authenticated before calling this function
- The function silently catches all exceptions and returns an empty list - check logs or add additional error handling if needed
- The folder_name parameter is case-sensitive and should match exactly as it appears in SharePoint
- The function retrieves ALL documents from root first, which may be inefficient for large document libraries - consider using SharePoint's native folder query capabilities for better performance
- The filtering logic checks for two patterns: exact folder prefix and folder name at first level - verify this matches your folder structure requirements
- Consider adding pagination support if dealing with large numbers of documents
- The function assumes documents have a 'relative_path' key - ensure your SharePoint client returns documents in this format
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function search_for_folders 68.8% similar
-
function get_root_folders 66.4% similar
-
function test_folder_structure 64.5% similar
-
function analyze_structure 61.9% similar
-
function search_and_locate 59.6% similar