Copy Navigation Using PowerShell

In this blog, we have discussed the copy of the navigation menu from one site to another site using a PowerShell script in SharePoint online. There are 3 types of Navigation in SharePoint Online,

  1. Site Navigation (Local Navigation)
  2. Global Navigation
  3. Hub Site (Tenant) Navigation).

Site Navigation

Site Navigation is also known as the Quick Launch Navigation or Local navigation. A user can access anything related to this site within one click by using this navigation. If you have a Team Site, it has navigation on the left side of the site. If you have a communication site the Quick Launch is on the top of the site.

Global Navigation (Tenant Navigation)

In Global Navigation users can access to any site in the same site collection.

Hub Navigation

Hub site navigation appears at the top of the site and it is global navigation. One would create a link in the sites that are part of the hub site.

In this blog, we will learn how to copy the navigations from one site to another using PowerShell PnP.

Copy hub Navigation

Follow the below code to copy Hub navigation from one site to another site.

Param(
    [Parameter(ParameterSetName = "Inputparameter", Position = 1, Mandatory = $True)]
    [String] $sourceHubsiteUrl,
    [Parameter(ParameterSetName = "Inputparameter", Position = 2, Mandatory = $True)]
    [String] $destinationHubsiteUrl)
Add - Type - AssemblyName PresentationFramework
Function Copy - NavigationToplink($sourceHubsiteUrl, $destinationHubsiteUrl) {
    $getTopNavs = Get - NavigationToplink - Hubsite $sourceHubsiteUrl
    Add - NavigationToplink - Topnavs $getTopNavs - Hubsite $destinationHubsiteUrl
}
Function Get - NavigationToplink($hubsite, $Credentials) {
    [System.Windows.MessageBox]::Show("Please provide credentials for login to the ($hubsite) Site")
    Connect - PnPOnline - Url $hubsite - UseWebLogin
    $topNavs = Get - PnPNavigationNode - Location TopNavigationBar | Select Title, Url
    Disconnect - PnPOnline
    return $topNavs
}
Function Add - NavigationToplink($topNavs, $hubsite) {
    [System.Windows.MessageBox]::Show("Please provide credentials for login to the ($hubsite) Site")
    Connect - PnPOnline - Url $hubsite - UseWebLogin
    foreach($topNav in $topNavs) {
        if ($TopNav.Url) {
            $navUrl = $topNav.Url
            $navTitle = $topNav.Title
            Add - PnPNavigationNode - Location TopNavigationBar - Title $navTitle - Url $navUrl
        }
    }
}
Copy - NavigationToplink - SourceHubsiteUrl $sourceHubsiteUrl - DestinationHubsiteUrl $destinationHubsiteUrl

Source site image

Destination site image

Copy quick launch Navigation

Follow the below code to copy the quick launch from one site to another site.

$srcsiteUrl = Read - Host "Enter sorce site url"
$destSiteUrl = Read - Host "Enter destination site url"
$credential = Get - Credential
#connect source site url
Connect - PnPOnline - Url $srcsiteUrl - Credentials $credential
$TopNavs = Get - PnPNavigationNode - Location QuickLaunch | Select Title, Url
#connect destination site url
Connect - PnPOnline - Url $destSiteUrl - Credentials $credential
foreach($TopNav in $TopNavs) {
    if ($TopNav.Url) {
        Add - PnPNavigationNode - Location QuickLaunch - Title $TopNav.Title
    }
}

Source Site Image

Destination site image

Conclusion

The above-mentioned script can be used to copy all types of Navigation from one site to another which can be really a great time saver script. If some navigation is getting changed in the site as per the requirement of the organization, then one can use this script to copy all the navigation options from one site to another which is really helpful.