当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:如何使用 PowerShell 批准多个文档?

作者:精品下载站 日期:2024-12-14 15:03:49 浏览:13 分类:玩电脑

SharePoint Online:如何使用 PowerShell 批准多个文档?


要求:使用 PowerShell 批准 SharePoint Online 中的多个文档。

PowerShell 是一个功能强大的工具,可以帮助您自动执行 SharePoint Online 中的常见任务。在这篇博文中,我们将向您展示如何使用 PowerShell 在 SharePoint Online 中同时批准多个文档。如果您需要批准大量文档,这可能是一个有用的节省时间的提示。

如何在 SharePoint Online 中批准多个项目?

当内容批准打开时,每个项目都必须经过批准才能对用户可见。您可以通过右键单击文档 >> 单击更多 >> 批准/拒绝 >> 单击“批准”来批准 SharePoint Online 中的任何项目或文档。

[玩转系统] SharePoint Online:如何使用 PowerShell 批准多个文档?

在 SharePoint Online 中批准文档可能是一个耗时的过程,尤其是当您有大量文档需要审阅时。幸运的是,SharePoint 提供了一种使用批量批准功能一次批准多个文档的方法。

要批准多个项目,您可以选择多个项目,然后单击命令栏或上下文菜单中的“批准/拒绝”按钮!

[玩转系统] SharePoint Online:如何使用 PowerShell 批准多个文档?

使用 PowerShell 批准 SharePoint Online 文档:

让我们使用 PowerShell 批准 SharePoint Online 中的单个文件:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Parameters
$SiteURL = "https://Crescent.sharepoint.com"
$FileRelativeURL ="/Shared Documents/Compliance Process.xlsx"
$ApprovalComments= "Approved by the Admin"

#Get credentials to connect
$Cred = Get-Credential

#Setup the Context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName, $Cred.Password)
 
#Get the File and Approve
$File = $Ctx.Web.GetFileByServerRelativeUrl($FileRelativeURL)
$File.Approve($ApprovalComments)
$Ctx.ExecuteQuery()
 
Write-Host -f Green "File Approved Successfully!"

同样,要拒绝文件,请使用:$File.Deny($Comment)。我们可以使用 PowerShell 脚本来批准 SharePoint Online 中的多个项目。

SharePoint Online:使用 PowerShell 批准多个文档

让我们使用 PowerShell 批准列表或文档库中的所有待处理项目:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Parameters
$SiteURL = "https://Crescent.sharepoint.com"
$ListName ="Documents"

#Get credentials to connect
$Cred = Get-Credential

#Setup the Context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName, $Cred.Password)
 
#Get the File and Approve
$List = $Ctx.web.Lists.GetByTitle($ListName)

#Query to get all items with status other than "Approved"
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "<View Scope = 'RecursiveAll'><Query><Where><Neq><FieldRef Name='_ModerationStatus' /><Value Type='ModStat'>0</Value></Neq></Where></Query></View>"
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()

#Approve all pending List Items
ForEach($Item in $ListItems)
{
    $Item["_ModerationStatus"] = 0
    $Item.Update()
    $Ctx.ExecuteQuery()
    Write-host -f Green "Approved List Item:" $Item.Id
} 

PnP PowerShell 批准文档库中的所有文件

要批准 SharePoint Online 文档库中的所有文件,请使用以下 PowerShell 脚本:


#Parameters
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$ListName = "Branding"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get All Files in Draft State
$DraftItems = Get-PnPListItem -List $ListName -PageSize 2000 | Where {$_.FileSystemObjectType -eq "File" -and  $_["_ModerationStatus"] -ne 0}

#Approve Files
$DraftItems | ForEach-Object { 
    Set-PnPListItem -List $ListName -Identity $_ -Values @{"_ModerationStatus"=0;"_ModerationComments"="Approved by Script"} | Out-Null
    Write-host "Approved File:"$_.FieldValues.FileRef
}

这里,审核状态字段具有以下枚举:

  • 已批准:0
  • 被拒绝:1
  • 待处理:2
  • 草稿:3
  • 预定:4

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯