ค้นหาเว็บไซต์

SharePoint Online: จะตรวจสอบเอกสารโดยใช้ PowerShell ได้อย่างไร


ฟีเจอร์เช็คเอาท์และเช็คอินใน SharePoint หลีกเลี่ยงความขัดแย้งในสภาพแวดล้อมการทำงานร่วมกันโดยทั่วไป ซึ่งผู้ใช้หลายคนอาจพยายามแก้ไขเอกสารเดียวกันพร้อมกัน เมื่อคุณทำการเปลี่ยนแปลงเสร็จแล้ว คุณต้องเช็คอินเอกสารกลับเพื่อให้การเปลี่ยนแปลงพร้อมใช้งานสำหรับผู้ใช้รายอื่น แม้ว่าเอกสารจะถูกบันทึกแล้วก็ตาม การเช็คอินเอกสารใน SharePoint Online เป็นกระบวนการง่ายๆ และสามารถทำได้ในไม่กี่ขั้นตอนผ่าน UI ของเว็บเบราว์เซอร์หรือ PowerShell

จะตรวจสอบเอกสารใน SharePoint Online ได้อย่างไร

ทำตามขั้นตอนเหล่านี้ใน SharePoint Online เพื่อเช็คอินไฟล์ที่เช็คเอาท์

  1. นำทางไปยังไลบรารีเอกสารที่บันทึกไฟล์ที่เช็คเอาท์ไว้ เลือกเอกสารที่จะเช็คอิน
  2. จาก Ribbon ให้คลิกแท็บไฟล์ และคลิกที่ "เช็คอิน" จากกลุ่ม "เปิดและเช็คเอาท์"
  3. ในกล่องโต้ตอบเช็คอิน คลิกไม่สำหรับตัวเลือก "เก็บไฟล์ไว้หลังจากเช็คอิน"
  4. คลิกตกลง

หากคุณเช็คอินเอกสารแต่เก็บไว้เพื่อตรวจสอบ ผู้ใช้รายอื่นจะได้เห็นการเปลี่ยนแปลงล่าสุดของคุณ ในขณะที่คุณอาจดำเนินการแก้ไขส่วนที่เหลือในเอกสารของคุณต่อไปได้

หากไม่มีตัวเลือก “เช็คอิน” แสดงว่าเอกสารยังไม่ได้เช็คเอาท์ ในประสบการณ์สมัยใหม่ คุณสามารถเลือกไฟล์ที่เช็คเอาท์ >> คลิกขวาและเลือก "เช็คอิน" จากเมนูบริบทหรือจากแถบเครื่องมือ

แม้ว่าสิ่งนี้จะมีประโยชน์ แต่ถ้าคุณต้องการทำให้กระบวนการนี้เป็นไปโดยอัตโนมัติล่ะ PowerShell เป็นวิธีที่จะไป!

SharePoint Online PowerShell CSOM เพื่อเช็คอินเอกสาร

ฉันจะแสดงวิธีเช็คอินเอกสารใน SharePoint Online โดยใช้ PowerShell:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Function to Check if file exists in given URL
Function Checkin-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 checked out
        If($File.CheckOutType -eq "None")
        {
            write-host "Document is not checked out Already!" -f Red
            break
        }
        #Checkin the document
        $File.CheckIn([string]::Empty,[Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
        #Checkin Types: MajorCheckIn,MinorCheckIn,OverwriteCheckIn

        $Ctx.ExecuteQuery()

        write-host "Document has been checked-in successfully!" -f Green
    }
    Catch {
        write-host -f Red "Error checking-in 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.xssn"

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

#Call the function to Checkin file
Checkin-Document -SiteURL $SiteURL -FileRelativeURL $FileRelativeURL -Credentials $Cred

SharePoint Online PowerShell เพื่อเช็คอินไฟล์

สคริปต์ด้านบนได้รับไฟล์จาก URL หรือคุณสามารถรับไฟล์จาก Item ID และเช็คอินได้ ต่อไปนี้เป็นอีกวิธีหนึ่งของ PowerShell ในการเช็คอินไฟล์ใน SharePoint Online


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\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

#Get the File to context
$Ctx.Load($File)
$Ctx.ExecuteQuery()
        
#determine if sharepoint document is checked out
If($File.CheckOutType -eq "None")
{
    write-host "Document is not checked out Already!" -f Red
}
else
{
    #Check in the file: sharepoint online force check in powershell 
    $File.CheckIn("Checked-in from PowerShell",[Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
    $Ctx.ExecuteQuery()
    
    write-host "Document has been checked-in successfully!" -f Green
}

SharePoint Online: PnP PowerShell เพื่อเช็คอินเอกสาร

PnP PowerShell ทำให้ง่ายขึ้นมาก! นี่คือ PowerShell สำหรับเช็คอินไฟล์ใน SharePoint Online:


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

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

#Check In a Document
Set-PnPFileCheckedIn -Url $FileRelativeURL -CheckinType MajorCheckIn -Comment "Checked In at $(Get-Date)" 

คุณสามารถดูผลการเช็คอินได้ที่หน้าประวัติเวอร์ชัน:

การตรวจสอบจำนวนมากในไฟล์ SharePoint Online โดยใช้ PowerShell:

หากต้องการเช็คอินหลายไฟล์ใน SharePoint Online โดยใช้ PowerShell ให้ใช้: SharePoint Online: PowerShell เพื่อตรวจสอบเอกสารทั้งหมด

บทความที่เกี่ยวข้อง