SharePoint Online: ตรวจสอบว่ามีรายการอยู่หรือไม่โดยใช้ PowerShell
ความต้องการ: ตรวจสอบว่ามีรายการเฉพาะอยู่ในไซต์ SharePoint Online ที่กำหนดโดยใช้สคริปต์ PowerShell CSOM
สคริปต์ PowerShell เพื่อตรวจสอบว่ามีรายการอยู่ใน SharePoint Online หรือไม่:
คุณจำเป็นต้องตรวจสอบว่ามีรายการ SharePoint หรือไลบรารีเอกสารก่อนที่จะสร้างหรือไม่ PowerShell สามารถช่วยได้! ในโพสต์นี้ เราจะแสดงวิธีใช้ PowerShell เพื่อพิจารณาว่ามีรายการอยู่หรือไม่ มันจะค้นหารายการตามชื่อและส่งกลับข้อความระบุว่าพบรายการหรือไม่
นี่คือ 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"
#Custom Function to Check if Site Collection Exists in Given URL
Function Check-ListExists($SiteURL, $ListName, $Credentials)
{
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
Try {
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()
Return $True
}
Catch [Exception] {
Write-host $_.Exception.Message -f Red
Return $False
}
}
#Set Variables for Site URL and List Name
$URL= "https://crescent.sharepoint.com/sites/sales/"
$List="Documents"
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
#Call the function to Check List Exists in given web
$ListExists = Check-ListExists -SiteURL $URL -ListName $List -Credentials $Cred
if($ListExists) {
write-host "List Exists in Given Site!!" -f Green
#Proceed with your script
}
else {
write-host "List Doesn't Exists on given web!" -f Red
}
สิ่งนี้มีประโยชน์หากคุณต้องการทำงานอัตโนมัติหรือสร้างเวิร์กโฟลว์ที่ต้องตรวจสอบการมีอยู่ของไฟล์ก่อนดำเนินการบางอย่าง
วิธีที่ 2: PowerShell เพื่อตรวจสอบว่ามีรายการอยู่ใน SharePoint Online หรือไม่
แทนที่จะใช้วิธี "Try-Catch" ให้มาตรวจสอบว่าไซต์ได้ให้รายการโดยใช้ชื่อรายการหรือไม่
#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 a list or document library exists
Function Check-SPOListExists([Microsoft.SharePoint.Client.ClientContext]$Ctx, [String]$ListName)
{
Try {
#Get All lists from the web
$Lists = $Ctx.Web.Lists
$Ctx.Load($Lists)
$Ctx.ExecuteQuery()
#Check if the given List exists
$List = $Lists | where {$_.Title -eq $ListName}
If($List -ne $Null)
{
Return $True
}
Else
{
Return $False
}
}
Catch {
Write-host -f Red $_.Exception.Message
Return $False
}
}
#Config Parameters
$SiteUrl = "https://crescent.sharepoint.com/"
$ListName="Project Documents"
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Set up the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Ctx.Credentials = $credentials
#Call the function to Check List Exists in given web
$ListExists = Check-SPOListExists -Ctx $Ctx -ListName $ListName
If($ListExists -eq $True)
{
Write-Host -f Green "List '"$ListName"' exists!"
#Do Something with the List
#$List.Recycle()
#$Ctx.ExecuteQuery()
}
Else
{
Write-Host -f Red "List '"$ListName"' does not exists!"
}
PnP PowerShell เพื่อตรวจสอบว่ามีรายการอยู่ใน SharePoint Online หรือไม่
หนึ่งในวิธีที่ง่ายที่สุดในการตรวจสอบว่ามีรายการอยู่ใน SharePoint Online โดยใช้ PowerShell หรือไม่คือการใช้ Get-PnPList cmdlet ฉันจะแสดงวิธีใช้ PnP PowerShell เพื่อพิจารณาว่ามีรายการอยู่หรือไม่ จากนั้นจึงสร้างขึ้นหากจำเป็น:
#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Migration Tasks"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Try to Get the List or document library
$List = Get-PnPList -Identity $ListName -ErrorAction SilentlyContinue
If($List -ne $Null)
{
Write-Host -f Green "List Exist!"
}
Else
{
Write-Host -f Yellow "List Does Not Exist!"
#Create the list
}
หากมีรายการอยู่ คุณจะเห็น "รายการมีอยู่" หากไม่มีรายการ คุณจะเห็นข้อความ "ไม่มีรายการ"
โดยสรุป มีหลายวิธีในการตรวจสอบว่ามีรายการอยู่ใน SharePoint Online โดยใช้ PowerShell หรือไม่ คุณสามารถใช้ Get-PnPList cmdlet, สคริปต์ CSOM PowerShell หรือใช้วิธี Try-Catch เพื่อตรวจสอบว่ามีรายการอยู่หรือไม่ ด้วยการใช้สคริปต์เหล่านี้ คุณจะสามารถตรวจสอบได้อย่างรวดเร็วและง่ายดายว่ามีรายการอยู่หรือไม่
