Tìm kiếm trang web

SharePoint Online: PowerShell để có được tất cả các trang con trong tuyển tập trang


Yêu cầu: PowerShell để liệt kê tất cả các trang con trong bộ sưu tập Trang web SharePoint Online.

Làm cách nào để có được danh sách các trang con trong trang SharePoint Online?

Có một số cách khác nhau để bạn có thể liệt kê tất cả các site con trong tuyển tập site trong SharePoint Online. Một lựa chọn là sử dụng trang nội dung trang.

  1. Điều hướng đến trang web >> Nhấp vào cài đặt >> Chọn “Nội dung trang web”.
  2. Trong trang nội dung trang, nhấp vào “Trang con” (phím tắt URL: /_layouts/15/viewlsts.aspx?view=15). Trang này sẽ hiển thị danh sách tất cả các site con trong tuyển tập site hiện tại.

Tuy nhiên, trang này chỉ liệt kê các trang con trực tiếp của trang đó! Nếu có bất kỳ trang con cấp 2 nào, bạn sẽ không thấy chúng trên trang này.

SharePoint Online: Nhận tất cả các trang con bằng PowerShell

Làm cách nào để có được tất cả các trang con của tuyển tập trang trong SharePoint Online? Với tư cách là quản trị viên SharePoint, bạn có thể cần có danh sách tất cả các trang con trong môi trường SharePoint Online của mình, việc này có thể được thực hiện bằng PowerShell. Bài đăng blog này sẽ chỉ cho bạn cách sử dụng PowerShell để lấy danh sách tất cả các site con trong site SharePoint Online của bạn.

Để có được tất cả các trang con của một trang (trang cấp cao nhất hoặc trang phụ), hãy sử dụng tập lệnh PowerShell này:


#Requirement: sharepoint online get all subsites 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"

#Variables
$SiteURL = "https://crescent.sharepoint.com/Sites/Marketing"

Try {
    #Get Credentials to connect
    $Cred= Get-Credential

    #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 web
    $Web = $Ctx.web
    $Ctx.Load($Web)
    
    #get all subsites in sharepoint online powershell
    $Ctx.Load($Web.Webs)
    $Ctx.executeQuery()

    #Loop through each subsite and get subsite URL
    ForEach ($Subweb in $web.Webs)  
    {
       Write-host $Subweb.url  
    }  
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

Tập lệnh PowerShell này nhận tất cả các trang con ngay lập tức (hoặc các trang con cấp 1) của một trang nhất định. Làm thế nào để liệt kê tất cả các trang con theo cách đệ quy?

PowerShell để lấy tất cả các trang con trong Tuyển tập trang

Đây là SharePoint Online PowerShell để nhận tất cả các trang con (Web) theo cách đệ quy:


#Load SharePoint Online 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
$SiteUrl = "https://crescent.sharepoint.com/sites/Sales/"
$UserName="Salaudeen@crescent.com"
$Password ="Password goes here"
 
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
 
#sharepoint online powershell get subsites
Function Get-SPOWeb() {
param(
    $WebURL = $(throw "Please Enter the Site Collection URL")
)

    #Get Web information and subsites
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($webURL)
    $Context.Credentials = $credentials
    $Web = $context.Web
    $Context.Load($web)

    #powershell cmdlet to retrieve sharepoint online subsites
    $Context.Load($web.Webs) 
    $Context.executeQuery()

    #Do something with the current sub-site
    Write-host $Web.URL

    #Iterate through each subsite in the current web
    foreach ($Subweb in $web.Webs)
    {
        #Call the function recursively to process all subsites underneath the current web
        Get-SPOWeb($Subweb.url)
    }
}

#Call the function
Get-SPOWeb -WebURL $SiteUrl

PowerShell này nhận được tất cả các site con từ một tuyển tập site nhất định. Điều này có thể hữu ích nếu bạn cần một báo cáo về tất cả các site con trong tuyển tập trang hoặc nếu bạn cần thực hiện một số hành động trên tất cả chúng cùng một lúc. Ví dụ: lặp qua tất cả các trang con và thay đổi logo: SharePoint Online: Làm cách nào để thay đổi logo trang web bằng PowerShell?

Nhận các trang con của một trang web bằng lệnh ghép ngắn PowerShell PnP Get-PnPSubWeb

Chúng tôi cũng có thể nhận các Trang con trong SharePoint Online bằng PowerShell Pnp. Đây là PnP PowerShell để lấy tất cả các site con trong tuyển tập site trong SharePoint Online.


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred

    #Get subsites in SharePoint Online using PnP PowerShell
    $WebsCollection = Get-PnPSubWeb -Recurse
    
    #Iterate through each subsite
    ForEach($Web in $WebsCollection)
    {
        Write-host $Web.Url
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

Khóa chuyển -Recurse trong lệnh ghép ngắn Get-PnPSubWeb nhận tất cả các trang con từ tất cả các cấp độ của tuyển tập trang theo cách đệ quy; nếu bạn muốn bao gồm trang gốc của tuyển tập trang, hãy sử dụng: khóa chuyển -IncludeRootWeb.


#Parameter
$SiteURL = "https://crescent.sharepoint.com/sites/marketing" 

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
 
    #Get all webs in SharePoint Online site using PnP PowerShell
    $WebsCollection = Get-PnPSubWeb -Recurse -IncludeRootWeb
     
    #Iterate through each web in site
    ForEach($Web in $WebsCollection)
    {
        Write-host $Web.Url
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

Để có được tất cả các site và site con trong SharePoint Online bằng PowerShell, hãy sử dụng: Nhận tất cả các site và site con trong SharePoint Online bằng PowerShell

Bài viết liên quan