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

[玩转系统] SharePoint Online:如何使用 PowerShell 发布文件?

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

SharePoint Online:如何使用 PowerShell 发布文件?


要求:在 SharePoint Online 中发布文档。

如何在 SharePoint Online 中发布文件?

当您在 SharePoint Online 中启用次要版本(或草稿)时,具有只读权限的用户在发布任何文档之前都无法对其进行更改。发布的文档成为主要版本,例如1.0。以下是在 SharePoint Online 中发布文件的方法:

  1. 导航到文件所在的文档库。
  2. 右键单击要发布的文档 >> 在上下文菜单中,选择更多 >>,然后单击发布。

    [玩转系统] SharePoint Online:如何使用 PowerShell 发布文件?

  3. 在评论框中输入评论,然后单击确定。

它发布了一个主要版本,其他人现在就可以查看该文件!

使用 PowerShell 在 SharePoint Online 中发布文档

以下是从 SharePoint Online 发布文件所需的 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"

#Set Parameters
$SiteURL="https://Crescent.sharepoint.com/sites/marketing"
$FileRelativeURL ="/Sites/Marketing/Team Documents/Project Proposal.docx"
$Comment="Final Version"

#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 the File to Publish
    $File = $Ctx.Web.GetFileByServerRelativeUrl($FileRelativeURL)
    $Ctx.Load($File)
    $Ctx.ExecuteQuery()

    #Publish the File
    $File.Publish($Comment)
    $Ctx.ExecuteQuery()       

    Write-Host -f Green $File.Name "has been published successfully!"
}
Catch {
Write-host -f Red "Error:" $_.Exception.Message
}

同样,您可以使用“$File.Unpublish($Comment)”方法取消发布文件。

如何使用 PnP PowerShell 在 SharePoint Online 中发布主要版本?

要将次要版本文件发布到主要版本,请使用以下 PnP PowerShell 脚本:


#Set Parameters
$SiteURL="https://crescent.sharepoint.com/sites/retail"
$FileServerRelativeURL ="/sites/Retail/Invoices V5/V1 Template.docx"

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

#Get the File
$File = Get-PnPFile -Url $FileServerRelativeURL -ErrorAction SilentlyContinue
$FileLevel = Get-PnPProperty -ClientObject $File -Property Level

If($File)
{
    #Check if file draft (Minor version)
    If($File.Level -eq "Draft")
    {
        $File.Publish("Major version Published by Script")
        $File.Update()
        Invoke-PnPQuery
        Write-host -f Green "File Published Successfully!"
    }
    Else
    {
        Write-host -f Yellow "File is not in draft Mode!"
    }
}
Else
{
    Write-host -f Yellow "File Doesn't Exists!"
}

要一次性发布 SharePoint Online 文档库中所有文档的主要版本,请使用以下 PowerShell 脚本:


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/retail" 
$ListName = "Invoices V5"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
    
#Get all files from the document library
$ListItems = Get-PnPListItem -List $ListName -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
    $Level = Get-PnPProperty -ClientObject $File -Property Level

    #Check if file draft (Minor version)
    If($File.Level -eq "Draft")
    {
        $File.Publish("Major version Published by Script")
        $File.Update()
        Invoke-PnPQuery
        Write-host -f Green "Published file at '$($File.ServerRelativeUrl)'"
    }
}

如果签出草稿文件或启用内容审批会怎样?好吧,这是该场景批量发布的另一篇文章:SharePoint Online:使用 PowerShell 发布多个文档

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

取消回复欢迎 发表评论:

关灯