Create a SharePoint Online site map using PowerShell

 

Here is a PowerShell script that utilizes the SharePoint PnP, which will create a list of the subsites of a SharePoint site, creating a crude site map.

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12

$SPAdminURL = "https://contoso-admin.sharepoint.com"

Connect-SPOService -Url $SPAdminURL

 

# Load SharePoint PnP PowerShell module

Import-Module PnP.Powershell

 

# Specify the URL of the site to generate a site map for

$siteUrl = "https://contoso.sharepoint.com/"

 

# Connect to SharePoint Online using an app-only authentication context

Connect-PnPOnline -Url $siteUrl -Interactive

 

 

# Get the root site and all subsites

$sites = Get-PnPSubWeb -Recurse

 

# Create a hashtable to store the site map

$siteMap = @{}

 

# Recursively add each subsite to the site map

foreach ($site in $sites) {

    $siteMap[$site.Url] = $site.Title

    if ($site.Webs.Count -gt 0) {

        Get-SubSite $site.Url $siteMap

    }

}

 

# Define a function to recursively add each subsite to the site map

function Get-SubSite($url, $siteMap) {

    $subSites = Get-PnPSubWeb -WebUrl $url

    foreach ($subSite in $subSites) {

        $siteMap[$subSite.Url] = $subSite.Title

        if ($subSite.Webs.Count -gt 0) {

            Get-SubSite $subSite.Url $siteMap

        }

    }

}

 

# Output the site map as an HTML table

$html = "<table>"

foreach ($url in $siteMap.Keys) {

    $title = $siteMap[$url]

    $html += "<tr><td><a href='$url'>$title</a></td></tr>"

}

$html += "</table>"

 

$table = "Site map for site $($siteUrl):$($html)" | Out-File -FilePath C:\SWSetup\SiteMap.html

 

# Disconnect from SharePoint Online

Disconnect-PnPOnline

Share on Google Plus

About Tom DeMeulenaere

Highly accomplished information technology professional with extensive knowledge in System Center Configuration Manager, Windows Server, SharePoint, and Office 365.
    Blogger Comment

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.