SharePoint Online: Get Subsites using PowerShell
Requirement: Get a subsite using PowerShell in SharePoint Online
Get a Subsite using PowerShell in SharePoint Online
Here is how to get a subsite from PowerShell in SharePoint Online:
SharePoint Online: PowerShell to Get Subsites of a site
Similarly, we can get subsites of a given site using PowerShell as
SharePoint Online: Get All Subsites Recursively using PowerShell
Get a Subsite using PowerShell in SharePoint Online
Here is how to get a subsite from PowerShell in 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" #Function to Get the Web Function Get-SPOWeb() { Param ( $WebURL = $(throw "Please Enter the Site URL:") ) Try { #sharepoint online powershell get web $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($WebURL) $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password) $Web = $Ctx.Web $Ctx.Load($web) $Ctx.executeQuery() Return $Web } Catch { write-host -f Red "Error:" $_.Exception.Message } } #Get Credentials to connect $Cred = Get-Credential #Get the Web $Web = Get-SPOWeb -WebURL "https://crescent.sharepoint.com/sites/marketing" #sharepoint online powershell get site title Write-host $Web.TitleThis is equivalent to Get-SPWeb cmdlet in SharePoint On-premises PowerShell.
SharePoint Online: PowerShell to Get Subsites of a site
Similarly, we can get subsites of a given site using PowerShell as
#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 Get Subsites of the Web Function Get-SPOSubSites() { Param ( $WebURL = $(throw "Please Enter the Site URL:") ) Try { #Get subsites of a site (1st Level or immediate childs) $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($WebURL) $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password) $Web = $Ctx.Web $Ctx.Load($web) $Ctx.Load($web.Webs) $Ctx.executeQuery() Return $Web.Webs } Catch { write-host -f Red "Error:" $_.Exception.Message } } #Get Credentials to connect $Cred = Get-Credential #Get All Subsites of the Web $SubSites = Get-SPOSubSites -WebURL "https://crescent.sharepoint.com/sites/marketing" #Get the URL of Each subsite ForEach($Site in $SubSites) { Write-host $Site.Url }This script returns all immediate subsites of a given site. How about getting all subsites recursively?
SharePoint Online: Get All Subsites Recursively using 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 Get all Subsites recursively Function Get-SPOSubSitesRecursively() { Param ( $WebURL = $(throw "Please Enter the Site URL:") ) Try { $SubSites= @() #Get subsites of a site $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($WebURL) $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password) $Web = $Ctx.Web $Ctx.Load($Web) $Ctx.Load($Web.Webs) $Ctx.executeQuery() $SubSites += $Web #Call the function recursively on each subsite ForEach($Web in $Web.Webs) { $SubSites += Get-SPOSubSitesRecursively $Web.Url } Return $SubSites } Catch { write-host -f Red "Error:" $_.Exception.Message } } #Get Credentials to connect $Cred = Get-Credential #Get All Subsites of the Web $SubSites = Get-SPOSubSitesRecursively -WebURL "https://crescent.sharepoint.com/sites/marketing" #Get the URL of Each subsite ForEach($Site in $SubSites) { Write-host $Site.Url }Related Posts:
No comments:
Please Login and comment to get your questions answered!