SharePoint Online: PowerShell para obtener todos los subsitios de una colección de sitios
Requisito: PowerShell para enumerar todos los subsitios en una colección de sitios de SharePoint Online.
¿Cómo obtener una lista de subsitios en un sitio de SharePoint Online?
Hay algunas formas diferentes de enumerar todos los subsitios de una colección de sitios en SharePoint Online. Una opción es utilizar la página de contenido del sitio.
- Navegue hasta el sitio >> Haga clic en configuración >> Elija "Contenido del sitio".
En la página de contenido del sitio, haga clic en "Subsitios" (atajo de URL: /_layouts/15/viewlsts.aspx?view=15). Esta página mostrará una lista de todos los subsitios de la colección de sitios actual.
Sin embargo, esta página enumera solo los subsitios inmediatos del sitio. Si hay subsitios de segundo nivel, no los encontrará en esta página.
SharePoint Online: obtenga todos los subsitios mediante PowerShell
¿Cómo obtener todos los subsitios de una colección de sitios en SharePoint Online? Como administrador de SharePoint, es posible que necesite obtener una lista de todos los subsitios en su entorno de SharePoint Online, lo cual se puede hacer mediante PowerShell. Esta publicación de blog le mostrará cómo usar PowerShell para obtener una lista de todos los subsitios en su sitio de SharePoint Online.
Para obtener todos los subsitios de un sitio (ya sea un sitio de nivel superior o un subsitio), utilice este script de PowerShell:
#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
}
Este script de PowerShell obtiene todos los subsitios inmediatos (o subsitios de primer nivel) de un sitio determinado. ¿Cómo enumerar todos los subsitios de forma recursiva?
PowerShell para obtener todos los subsitios de una colección de sitios
Aquí está el PowerShell de SharePoint Online para obtener todos los subsitios (Web) de forma recursiva:
#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
Este PowerShell obtiene todos los subsitios de una colección de sitios determinada. Esto puede resultar útil si necesita un informe sobre todos los subsitios de una colección de sitios o si necesita realizar alguna acción en todos ellos a la vez. Por ejemplo, recorra todos los subsitios y cambie el logotipo: SharePoint Online: ¿Cómo cambiar el logotipo del sitio usando PowerShell?
Obtener subsitios de un sitio usando PowerShell PnP cmdlet Get-PnPSubWeb
También podemos obtener subsitios en SharePoint Online usando PowerShell Pnp. Aquí está PnP PowerShell para obtener todos los subsitios de una colección de sitios en 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
}
El modificador -Recurse del cmdlet Get-PnPSubWeb obtiene todos los subsitios de todos los niveles de la colección de sitios de forma recursiva; Si desea incluir el sitio raíz de la colección de sitios, utilice: el modificador -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
}
Para obtener todos los sitios y subsitios en SharePoint Online usando PowerShell, use: Obtener todos los sitios y subsitios en SharePoint Online usando PowerShell

