每隔x分钟基于文件名及时间条件从SharePoint获取文件的实现方案咨询
Got it, let's walk through how to build this automated SharePoint file fetcher with filtering—this is a common task, and there are solid, scalable ways to pull it off.
核心逻辑框架
At its core, you need three key components working together:
- Scheduled trigger: Runs your fetch logic every X minutes
- SharePoint data access: Securely connects to your target library and retrieves file metadata + content
- Filtering layer: Narrows down files by filename, creation/modification time, or both
工具选择(两种主流路径)
Pick the approach that fits your technical comfort and environment:
1. 代码化实现(灵活可控,适合开发者)
For this, Python is a great choice with the office365-rest-python-client library (it handles most SharePoint API heavy lifting). Alternatively, you could use C# with the Microsoft Graph SDK if you're in a .NET ecosystem.
2. 低代码实现(快速搭建,适合非开发者)
Microsoft Power Automate (or Power Apps) is perfect here—it has built-in SharePoint connectors and recurrence triggers, no coding required.
分步实现细节
第一步:Secure SharePoint Access
First, you need to authenticate to SharePoint:
- For code: Use App-Only permissions (recommended for automated tasks). Register an Azure AD app, grant it
Sites.Read.All(or more granular permissions) to your target site/library, then use the client ID + client secret to authenticate. - For Power Automate: Use the built-in SharePoint connector, just log in with your work account that has access to the library.
第二步:Set Up the Scheduled Trigger
- Python: Use libraries like
schedulefor simple intervals, orAPSchedulerfor more complex scheduling (like skipping weekends). Example snippet:import schedule import time def fetch_sharepoint_files(): # Your fetch/filter logic here pass # Run every X minutes schedule.every(30).minutes.do(fetch_sharepoint_files) while True: schedule.run_pending() time.sleep(60) - Power Automate: Add a "Recurrence" trigger, set the interval to X minutes.
第三步:Implement File Filtering
This is where you narrow down the files you want. Use SharePoint's OData query capabilities to filter directly at the API level (far more efficient than fetching all files and filtering locally):
Filter by Filename + Modification Time Example
OData Filter String:
$filter=substringof('your_keyword', FileLeafRef) and Modified ge datetime'2024-05-01T00:00:00Z'FileLeafRefis the internal SharePoint field for the filenamesubstringofchecks if your keyword exists in the filename (useeqfor exact matches)Modified gegets files modified on or after the specified timestamp
Python Code Snippet:
from office365.sharepoint.client_context import ClientContext from office365.runtime.auth.client_credential import ClientCredential import datetime site_url = "https://your-domain.sharepoint.com/sites/your-site" client_id = "your-client-id" client_secret = "your-client-secret" library_name = "Documents" ctx = ClientContext(site_url).with_credentials(ClientCredential(client_id, client_secret)) library = ctx.web.lists.get_by_title(library_name) # Define filter: filename contains "report" and modified in the last 30 minutes last_run_time = (datetime.datetime.now() - datetime.timedelta(minutes=30)).isoformat() + "Z" filter_query = f"substringof('report', FileLeafRef) and Modified ge datetime'{last_run_time}'" items = library.items.filter(filter_query).get().execute_query() for item in items: print(f"Found file: {item.properties['FileLeafRef']}, Modified: {item.properties['Modified']}") # Add code to download the file if neededPower Automate:
- Add a "Get files (properties only)" action
- Under "Filter Query", paste the OData filter string (e.g.,
substringof('report', FileLeafRef) and Modified ge '@{addMinutes(utcNow(), -30)}') - Use subsequent actions to download or process the filtered files
优化建议
- Incremental Fetching: Store the timestamp of your last run, then only fetch files modified/created after that time. This reduces API calls and speeds up the process.
- Error Handling: Add retry logic for network timeouts or authentication failures (in Python, use
tenacitylibrary; in Power Automate, use "Configure run after" to retry failed actions). - Logging: Track every run—log how many files were fetched, any errors, and timestamps. This helps with debugging if something breaks.
- Rate Limiting: SharePoint API has rate limits, so avoid hammering the endpoint if you have a large library. Add delays between requests if needed.
内容的提问来源于stack exchange,提问作者Henry

