SharePoint Online: วนซ้ำไซต์ย่อยทั้งหมดในไซต์คอลเลกชันโดยใช้ PowerShell
ความต้องการ: เรามักจะต้องวนซ้ำแต่ละไซต์ย่อยภายใต้ไซต์คอลเลกชันในขณะที่ทำงานกับ SharePoint Online โดยใช้ PowerShell
สคริปต์ PowerShell CSOM เพื่อวนซ้ำผ่านไซต์ย่อยทั้งหมดในไซต์คอลเลกชันใน SharePoint Online:
การวนซ้ำไซต์ย่อยทั้งหมดในคอลเลกชันไซต์ SharePoint Online โดยใช้ PowerShell เป็นสิ่งจำเป็นสำหรับการจัดการและทำให้สภาพแวดล้อม SharePoint Online ของคุณเป็นแบบอัตโนมัติ การวนซ้ำไซต์ย่อยทั้งหมดทำให้คุณสามารถดึงข้อมูลเกี่ยวกับไซต์ย่อยและดำเนินการต่างๆ ได้ เช่น การสร้างหรือการลบส่วนต่างๆ บทความนี้จะกล่าวถึงการใช้ PowerShell เพื่อวนซ้ำไซต์ย่อยทั้งหมดในไซต์คอลเลกชัน SharePoint Online
สคริปต์นี้จะวนซ้ำแต่ละไซต์ย่อยและแสดงรายการ URL ของไซต์ย่อยทั้งหมด:
#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"
#Variables for Processing
$SiteCollUrl = "https://crescent.sharepoint.com/sites/projects"
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Custom function to get all subsites in a given site collection
Function Get-SPOWebs($Url, $Credentials)
{
#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$Context.Credentials = $credentials
$web = $Context.Web
$Context.Load($web)
$Context.Load($web.Webs)
$Context.ExecuteQuery()
#Do Something Here
Write-host $Web.URL
#Process Each subsite in current site
ForEach($web in $web.Webs)
{
#call the function recursively
Get-SPOWebs $web.Url $Credentials
}
}
#call the function
Get-SPOWebs $SiteCollUrl $Credentials
ต่อไปนี้เป็นตัวอย่างในทางปฏิบัติของการวนซ้ำไซต์ย่อยทั้งหมดในไซต์คอลเลกชันและการเปลี่ยนโลโก้ในแต่ละไซต์ย่อย: จะเปลี่ยนโลโก้ใน SharePoint Online โดยใช้ PowerShell ได้อย่างไร
จะวนซ้ำไซต์ย่อยทั้งหมดใน SharePoint Online โดยใช้ PnP PowerShell ได้อย่างไร
เวอร์ชัน PnP PowerShell ของการวนซ้ำผ่านไซต์ย่อยจะเป็นเช่นนี้ด้วย Get-PnPSubWeb cmdlet:
#Parameter
$SiteURL = "https://crescent.sharepoint.com/sites/projects"
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get all subsites in a SharePoint Online site
$Subsites = Get-PnPSubWeb -Recurse -IncludeRootWeb
#Iterate through each subsite
ForEach($site in $Subsites)
{
Write-host $Site.Url
}
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}