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

[玩转系统] SharePoint Online:如何使用 PowerShell 签出文档?

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

SharePoint Online:如何使用 PowerShell 签出文档?


在 SharePoint 中签出文档可确保您是唯一对其进行编辑的人。签出的目的是防止协作环境中发生冲突,在协作环境中,多人可能会尝试同时编辑同一文档或列表项。因此,当您签出某个文件时,您是唯一允许对其进行更改的人,并且该文件将保持锁定状态,直到您释放它为止。

完成编辑后,签入文档,以便其他用户可以查看您所做的更改。可以从文档库设置中打开或关闭“需要签出”选项。这篇博文将介绍如何在 SharePoint Online 中使用 PowerShell 签出文档。

如何在 SharePoint Online 中签出文档?

要在 SharePoint Online 中查看文档,请执行以下步骤:

  1. 导航到 SharePoint 或 SharePoint Online 网站中的文档库。选择您要查看的文档。
  2. 右键单击该文件,然后从上下文菜单中选择“检出”。 (或者您也可以单击工具栏上的“签出”)
  3. 签出文档后,您将在文档图标中看到一个向外的绿色小箭头。

    [玩转系统] SharePoint Online:如何使用 PowerShell 签出文档?

用于在 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"
  
#Function to Check out a file from given URL
Function Checkout-Document($SiteURL, $FileRelativeURL, $Credentials)
{

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials

    Try {
        #Try to get the File from URL
        $File = $Ctx.web.GetFileByServerRelativeUrl($FileRelativeURL)
        $Ctx.Load($File)
        $Ctx.ExecuteQuery()
        
        #check if the file is already checked out
        If($File.CheckOutType -ne "None")
        {
            write-host "File is already checked-out!" -f Yellow
            break
        }
        #Checkout the document
        $File.CheckOut()
        $Ctx.ExecuteQuery()

        write-host "Document has been checked-out successfully!" -f Green
    }
    Catch {
        write-host -f Red "Error checking out Document!" + $_.Exception.Message
    }    
}

#Set Variables for Site URL, List Name and Column Name
$SiteURL= "https://crescent.sharepoint.com/sites/sales/"
$FileRelativeURL="/sites/Sales/Shared%20Documents/Legal%20Template.xsn"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

#Call the function to Checkout file
Checkout-Document -SiteURL $SiteURL -FileRelativeURL $FileRelativeURL -Credentials $Cred 
使用 PowerShell 在 SharePoint Online 中放弃结账:
如果您想放弃签出,请使用: $File.UndoCheckOut() 方法。

SharePoint Online:使用 PowerShell CSOM 签出文档

或者,您可能必须从项目 ID 而不是文档 URL 中签出。以下是从项目 ID 中检出文档的脚本:


#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 Variables for Site URL, Library Name and Item ID
$SiteURL= "https://crescent.sharepoint.com/sites/sales/"
$LibraryName="Documents"
$ItemID="4"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred

#Get the web, List, Item adn File
$Web=$Ctx.Web
$List=$web.Lists.GetByTitle($LibraryName)
$Item=$list.GetItemById($ItemID)
$File=$Item.File

#Check out the file
$File.CheckOut()
$Ctx.ExecuteQuery()

使用 PnP PowerShell 检出文件

以下是如何在 SharePoint Online 中使用 PnP PowerShell cmdlet Set-PnPFileCheckedOut 签出文档:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$FileRelativeURL ="/Team Documents/Project Proposal.docx"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Checkout the File
Set-PnPFileCheckedOut -Url $FileRelativeURL

要使用 PowerShell 签入文件,请使用:如何使用 PowerShell 在 SharePoint Online 中签入文档?

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

取消回复欢迎 发表评论:

关灯