Update SharePoint Navigation After Site Rename

Site renaming has finally arrived to SharePoint Online. Site rename allows you to change site’s url. After site rename redirect from old address to new address is created automatically, there is some issues and you should read the documentation carefully before executing. Here is the article, which also contains required steps: https://docs.microsoft.com/en-us/sharepoint/change-site-address.

I noticed that navigation links are not updated automatically. Redirect basically handles the situation and end-users can navigate normally, but sometimes typos or misleading url address might be confusing to the end user. If there is already a large navigation created, for example in organization’s intranet, it takes some time to update site address accordingly and operation is very prone to errors. Current navigation editing mechanism is not allowing mass-operations, so it requires going through navigation items pointing to renamed site one-by-one.

I wrote a simple PowerShell script, which can save your time and effort. Currently script can update top navigation, hub navigation and left side Quick Launch navigation.

<#

.SYNOPSIS
 
    Update-NavigationUrls.ps1 - Mass updated SharePoint Online navigation urls. 
   
 
.DESCRIPTION
    Author: Matti Paukkonen

    This script allows you to mass update SharePoint Online navigation urls, for example after renaming a site.
    Script utilized PnP PowerShell module, which needs to be installed.
    
.LINK
    Blog: https://mattipaukkonen.com
    Twitter: http://www.twitter.com/mpaukkon

#>
Param (

    [Parameter(mandatory = $true)][string]$SiteUrl,
    [Parameter(mandatory = $true)][string]$OldUrl,
   [Parameter(mandatory = $true)][string]$NewUrl,
   [Parameter(mandatory = $true)][ValidateSet('TopNavigationBar', 'QuickLaunch','Footer')][string]$Location

)

Function Set-NavigationNode
{
    param($navNode)
    if($navNode.Children.Count -gt 0)
    {
        foreach($childNode in $navNode.Children)
        {
            Set-NavigationNode $childNode
        }
    }
    if($navNode.Url -match $OldUrl)
    {
     Write-Host "Updating navigation node:" -NoNewline -ForegroundColor Yellow
     Write-Host $navNode.Title -ForegroundColor Yellow
     Write-host $navNode.Url -ForegroundColor Yellow
     $navNode.Url = $navNode.Url.ToLower().Replace($OldUrl.ToLower(),$NewUrl.ToLower())
     $navNode.Update()
    }
}

Write-Host "Connecting to site: $SiteUrl" -ForegroundColor Yellow
Connect-PnPOnline $SiteUrl -UseWebLogin
$site = Get-PnPSite -ErrorAction SilentlyContinue
if($site -ne $null)
{
Write-Host "Connected" -ForegroundColor Green
$navigationNodes = Get-PnPNavigationNode -Location $Location
foreach($navigationNode in $navigationNodes)
{
    $node = Get-PnPNavigationNode -Id $navigationNode.Id
    Set-NavigationNode $node
}
Invoke-PnPQuery
Disconnect-PnPOnline

}
else
{
    Write-Host "Connection to site failed!" -ForegroundColor Red
}

You can simply call it like this:

.\Update-NavigationUrls.ps1 -SiteUrl https://<tenant>.sharepoint.com/sites/<site> -OldUrl /sites/srvices -NewUrl /sites/services -Location TopNavigationBar

In the example, all urls containing “/sites/srvices” on the top navigation are updated to include “/sites/services”. SiteUrl parameter is the address to the site, where navigation needs to be updated. OldUrl is the part of the url, which needs to be updated, and NewUrl is the new replacement. I suggest including managed path (/sites or /teams) and complete site url to these parameter, like in my example. Location parameter can be either TopNavigationBar or QuickLaunch. If you want to update hub navigation items, you need to connect to the hub site and use TopNavigationBar as Location.

You can download a complete script from my Github repository: https://github.com/mpaukkon/SharePoint/blob/master/PowerShell/Update-NavigationUrls.ps1

Updates to the script are also made to the Github repo.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: