如何通过编程在SharePoint Online中启用文档的信息管理策略标签?
以下是使用PnP PowerShell和CSOM(C#)实现的具体方法:
PnP PowerShell 实现
# 连接到目标SharePoint Online站点 Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -Interactive # 获取目标文档内容类型(替换为实际内容类型名称) $contentType = Get-PnPContentType -Identity "Document" # 启用内容类型的文档标签功能 Set-PnPInformationManagementPolicy -ContentType $contentType -EnableLabels $true
- 若需指定默认标签,可添加
-DefaultLabelId "标签GUID"参数 - 执行前确保拥有站点管理员或内容类型管理权限
CSOM(C#)实现
需先安装依赖包:PnP.Framework 或 Microsoft.SharePointOnline.CSOM
using Microsoft.SharePoint.Client; using PnP.Framework; var siteUrl = "https://yourtenant.sharepoint.com/sites/yoursite"; var authManager = new AuthenticationManager(); using (var context = authManager.GetContextInteractive(siteUrl)) { // 获取目标内容类型 var contentType = context.Web.ContentTypes.GetByName("Document"); context.Load(contentType); context.ExecuteQuery(); // 获取信息管理策略实例 var policy = InformationManagementPolicy.GetPolicy(context, contentType); context.Load(policy); context.ExecuteQuery(); // 启用文档标签 policy.Properties["EnableLabels"] = "True"; // 可选:设置默认标签ID // policy.Properties["LabelId"] = "你的标签GUID"; policy.Update(); context.ExecuteQuery(); }
注意事项
- 仅文档类内容类型支持标签功能,需确保目标内容类型继承自"文档"基类型
- 修改信息管理策略需站点级权限,普通编辑权限无法操作
- 启用后需等待数分钟,策略设置才会同步到站点内的文档库
内容的提问来源于stack exchange,提问作者Andrei Popescu
相关产品推荐
相关产品推荐

