Teams 与 SharePoint 自动化
Microsoft Teams 和 SharePoint 是企业协作和文档管理的核心。通过 Power Automate + Graph API,可以实现强大的自动化。
自动化场景全景
graph TB
A[Teams & SharePoint 自动化] --> B[消息自动化]
A --> C[文档自动化]
A --> D[审批流程]
A --> E[数据同步]
B --> B1[频道通知]
B --> B2[机器人回复]
B --> B3[定时消息]
C --> C1[文档创建]
C --> C2[权限管理]
C --> C3[版本归档]
D --> D1[请假审批]
D --> D2[采购审批]
D --> D3[文档审批]
E --> E1[列表同步Excel]
E --> E2[项目进度更新]
style A fill:#e3f2fd,stroke:#1565c0,stroke-width:2px
style B fill:#c8e6c9,stroke:#43a047,stroke-width:2px
style D fill:#fff9c4,stroke:#f9a825,stroke-width:2px
Teams 消息自动化
from dataclasses import dataclass, field
from datetime import datetime
@dataclass
class TeamsMessage:
channel_id: str
content: str
mentions: list[str] = field(default_factory=list)
importance: str = "normal" # normal | urgent
card_template: str | None = None
@dataclass
class AdaptiveCard:
"""Teams 自适应卡片"""
title: str
body: list[dict]
actions: list[dict] = field(default_factory=list)
def to_payload(self) -> dict:
return {
"type": "AdaptiveCard",
"version": "1.4",
"body": [
{"type": "TextBlock", "text": self.title,
"weight": "bolder", "size": "medium"},
*self.body,
],
"actions": self.actions,
}
class TeamsAutomation:
"""Teams 自动化工具箱"""
def __init__(self, webhook_url: str = ""):
self.webhook_url = webhook_url
self.message_log: list[dict] = []
def create_daily_standup_card(
self, team: str, date: str, items: list[dict]
) -> AdaptiveCard:
"""生成每日站会卡片"""
body = []
for item in items:
body.append({
"type": "ColumnSet",
"columns": [
{"type": "Column", "width": "auto", "items": [
{"type": "TextBlock", "text": item["name"],
"weight": "bolder"}
]},
{"type": "Column", "width": "stretch", "items": [
{"type": "TextBlock", "text": item["status"],
"wrap": True}
]},
],
})
return AdaptiveCard(
title=f"📋 {team} 站会 - {date}",
body=body,
actions=[
{"type": "Action.OpenUrl", "title": "查看看板",
"url": "https://dev.azure.com"},
],
)
def create_approval_card(
self, title: str, requester: str, details: str
) -> AdaptiveCard:
"""生成审批请求卡片"""
return AdaptiveCard(
title=f"⏳ 审批请求: {title}",
body=[
{"type": "TextBlock", "text": f"申请人: {requester}"},
{"type": "TextBlock", "text": details, "wrap": True},
],
actions=[
{"type": "Action.Submit", "title": "✅ 批准",
"data": {"action": "approve"}},
{"type": "Action.Submit", "title": "❌ 拒绝",
"data": {"action": "reject"}},
],
)
SharePoint 文档管理自动化
from dataclasses import dataclass
from pathlib import Path
@dataclass
class SPDocument:
name: str
path: str
created_by: str
created_at: str
size_kb: int
version: int = 1
class SharePointAutomation:
"""SharePoint 文档管理自动化"""
FOLDER_TEMPLATES = {
"项目": ["01-计划", "02-执行", "03-交付", "04-归档"],
"部门": ["政策文件", "培训材料", "会议记录", "模板"],
"合同": ["草案", "审批中", "已签署", "已归档"],
}
def __init__(self):
self.documents: list[SPDocument] = []
def create_project_structure(
self, project_name: str, template: str = "项目"
) -> list[str]:
"""自动创建项目文件夹结构"""
folders = self.FOLDER_TEMPLATES.get(template, [])
created = []
for folder in folders:
path = f"/sites/projects/{project_name}/{folder}"
created.append(path)
return created
def auto_archive(
self, documents: list[SPDocument], days_threshold: int = 90
) -> list[SPDocument]:
"""自动归档过期文档"""
from datetime import datetime, timedelta
cutoff = datetime.now() - timedelta(days=days_threshold)
to_archive = []
for doc in documents:
doc_date = datetime.fromisoformat(doc.created_at)
if doc_date < cutoff:
to_archive.append(doc)
return to_archive
def generate_permission_report(
self, site: str, users: list[dict]
) -> list[dict]:
"""生成权限审计报告"""
report = []
for user in users:
report.append({
"site": site,
"user": user["name"],
"permission": user["level"],
"last_access": user.get("last_access", "未知"),
"risk": "高" if user["level"] == "Full Control"
and user.get("external", False) else "低",
})
return sorted(report, key=lambda r: r["risk"], reverse=True)
常见自动化场景
| 场景 | 触发器 | 工具 | 复杂度 | 预期收益 |
|---|---|---|---|---|
| 新员工入职文件夹 | HR系统触发 | Power Automate | 低 | 节省30分钟/人 |
| 合同审批流程 | 文件上传 | Power Automate + Teams | 中 | 审批提速 50% |
| 周报自动汇总 | 定时触发 | Power Automate + Excel | 中 | 节省2小时/周 |
| 文档过期归档 | 定时触发 | Graph API | 低 | 释放存储空间 |
| 权限定期审计 | 月度定时 | Graph API + Power BI | 高 | 降低安全风险 |
本章小结
- Adaptive Card 是 Teams 自动化的核心——卡片可嵌入审批按钮和数据展示
- SharePoint 模板化——项目/部门/合同文件夹一键生成
- 权限审计自动化——定期生成报告,识别高风险外部用户
- 文档生命周期——自动归档过期文件,释放存储
- 审批流与消息联动——在 Teams 中完成审批,无需跳转