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

[玩转系统] SharePoint Online:使用 PowerShell 发布库中的所有文件

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

SharePoint Online:使用 PowerShell 发布库中的所有文件


要求:在 SharePoint Online 中批量发布多个文件。

[玩转系统] SharePoint Online:使用 PowerShell 发布库中的所有文件

如何在SharePoint Online中发布多个文件?

要在 SharePoint Online 中发布所有文档,您可以按照以下步骤操作:

  1. 转到存储文档的 SharePoint 文档库。
  2. 通过单击每个文档旁边的复选框来选择要发布的文档。您还可以通过设置过滤器来创建视图:批准状态等于草稿,并将视图设置为显示所有不带文件夹的文件。
  3. 从命令栏中选择“签入”。
  4. 将出现一个弹出窗口,允许您签入并添加注释,解释您对文档所做的更改。
  5. 选择“主要版本(发布)”以发布所有选定的文件并使其可供其他人使用。

    [玩转系统] 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"

#Set Parameters
$SiteURL="https://Crescent.sharepoint.com"
$ListName ="Team Documents"

#Get Credentials to connect
$Cred= Get-Credential

Try{
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Get All Items from the List
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()) 
    $Ctx.Load($List)
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()

    ForEach($ListItem in $ListItems)
    {
        #Approve the File if "Content Approval is Turned-ON"
        If ($List.EnableModeration -eq $true)
        {
            If ($ListItem["_ModerationStatus"] -ne '0')
            { 
                $ListItem.File.Approve("Approved by Admin")
                Write-Host "File Approved: "$ListItem["FileLeafRef"] -ForegroundColor Yellow
                $Ctx.ExecuteQuery()
            }
        }

        #Checkin the File if its checked-out
        If ($ListItem["CheckoutUser"] -ne $null)
        {
            $ListItem.File.CheckIn("Check-in by Admin", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
            Write-Host "File Checked in: "$ListItem["FileLeafRef"] -ForegroundColor Cyan
            $Ctx.ExecuteQuery()
        }

        #Publish the File
        If($List.EnableVersioning -and $List.EnableMinorVersions)
        {
            $ListItem.File.Publish("Published by Admin")
            $Ctx.ExecuteQuery()
            Write-Host -f Green "File published:" $ListItem["FileLeafRef"]
        }
    }
}
Catch {
Write-host -f Red "Error:" $_.Exception.Message
}
您还可以使用“内容和结构”在SharePoint Online中批量发布文档!

PnP PowerShell 发布文档库中的所有文件

您可以使用 PnP PowerShell 发布 SharePoint Online 文档库中的所有文档。以下是如何执行此操作的示例:


#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Retail"
$LibraryName= "Invoices"
  
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
#Get All Files from the Document Library
$List =  Get-PnPList -Identity $LibraryName
$ListItems = Get-PnPListItem -List $LibraryName -PageSize 2000 | Where { $_.FileSystemObjectType -eq "File" }

#Iterate through each file
ForEach ($Item in $ListItems)
{
    #Get the File from List Item
    $File = Get-PnPProperty -ClientObject $Item -Property File 

    #Approve the File if "Content Approval is Turned-ON"
    If ($List.EnableModeration -eq $true)
    {
        If ($Item["_ModerationStatus"] -ne '0')
        { 
            Set-PnPListItem -List $LibraryName -Identity $Item.ID -Values @{"_ModerationStatus"=0;"_ModerationComments"="Approved by Script"} | Out-Null            
            Write-host -f Green "Approved File:"$Item.FieldValues.FileRef
        }
    }

    #Check in the file, if its checked out
    If($File.CheckOutType -ne "None")
    {
        Set-PnPFileCheckedIn -Url $File.ServerRelativeUrl -CheckinType MajorCheckIn 
        Write-host -f Green "File Checked-In and Published:"$File.ServerRelativeUrl
    }
    Elseif($File.MinorVersion )# Check if file draft (Minor version)
    {
        $File.Publish("Major version Published by Script")
        $File.Update()
        Invoke-PnPQuery
        Write-host -f Green "Published File at '$($File.ServerRelativeUrl)'"
    }
}

顺便说一句,SPModerationStatusType 枚举具有以下值:

  • 已批准 - 0
  • 拒绝-1
  • 待定 - 2
  • 草案 - 3
  • 预定 - 4

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

取消回复欢迎 发表评论:

关灯