kwizcom banner advertisement

SharePoint Online: Update Quick Launch Links using PowerShell

Requirement: Update Quick Launch in SharePoint Online using PowerShell

How to Update Quick Launch Links in SharePoint Online?
To update links in quick launch,
  • Go to Site Settings >> Click on "Quick launch" from Look and feel
  • Click on "Edit" icon to edit and update quick launch link.
powershell to update quick launch links in sharepoint online

If publishing feature is enabled, You'll get "Navigation" link in site settings under "Look and Feel"
sharepoint online powershell update quick launch


SharePoint Online PowerShell to Update Quick Launch:
#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"
   
#Config Parameters
$SiteURL="https://crescenttech.sharepoint.com/sites/marketing"
$OldURL = "http://support.crescent.com/app"
$NewURL = "http://support.crescent.com/applications"

#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials

#Get the Quick launch Navigation of the web 
$QuickLaunch = $Ctx.Web.Navigation.QuickLaunch
$Ctx.load($QuickLaunch)
$Ctx.ExecuteQuery()

#Loop through Each link in Quick Launch Navigation
ForEach($QuickLaunchLink in $QuickLaunch)
{
    $Ctx.Load($QuickLaunchLink)
    $Ctx.Load($QuickLaunchLink.Children)
    $Ctx.ExecuteQuery()

    If($QuickLaunchLink.Url -eq $OldURL)
    {
        $QuickLaunchLink.Url = $QuickLaunchLink.Url.Replace($OldURL,$NewURL)
        $QuickLaunchLink.Update()
        $Ctx.ExecuteQuery()
        Write-Host -f Green "Quick Launch Link: '$($QuickLaunchLink.Title)' Updated!"
    }

    #Get Second level Links
    Foreach($Link in $QuickLaunchLink.Children)
    {
        $Ctx.Load($Link)
        $Ctx.ExecuteQuery()
          
        If($Link.Url -eq $OldURL)
        {
            $Link.Url = $Link.Url.Replace($OldURL,$NewURL)
            $Link.Update()
            $Ctx.ExecuteQuery()
            Write-Host  -f Green "Quick Launch Link '$($Link.Title)' Updated!"
        }
    }
} 

Update Quick Launch Link for All Site Collections
Lets wrap the script into a function and search and replace quick launch links for all site collections in SharePoint Online tenant.
#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 Update-SPOQuickLaunchLinks([Microsoft.SharePoint.Client.Web]$Web, $OldURL, $NewURL)
{   
    Try {
        Write-host -f Yellow "Searching Quick Launch Links in:" $($Web.url)
        $Ctx = $Web.Context
        #Get the Quick launch Navigation of the web 
        $QuickLaunch = $Web.Navigation.QuickLaunch
        $Ctx.load($QuickLaunch)
        $Ctx.ExecuteQuery()

        #Loop through Each link in Quick Launch Navigation
        ForEach($QuickLaunchLink in $QuickLaunch)
        {
            $Ctx.Load($QuickLaunchLink)
            $Ctx.Load($QuickLaunchLink.Children)
            $Ctx.ExecuteQuery()

            If($QuickLaunchLink.Url -eq $OldURL)
            {
                $QuickLaunchLink.Url = $QuickLaunchLink.Url.Replace($OldURL,$NewURL)
                $QuickLaunchLink.Update()
                $Ctx.ExecuteQuery()
                Write-Host -f Green "`tQuick Launch Link: '$($QuickLaunchLink.Title)' Updated!"
            }

            #Get Second level Links
            Foreach($Link in $QuickLaunchLink.Children)
            {
                $Ctx.Load($Link)
                $Ctx.ExecuteQuery()
          
                If($Link.Url -eq $OldURL)
                {
                    $Link.Url = $Link.Url.Replace($OldURL,$NewURL)
                    $Link.Update()
                    $Ctx.ExecuteQuery()
                    Write-Host  -f Green "`tQuick Launch Link '$($Link.Title)' Updated!"
                }
            }
        }
    }
    Catch {
        write-host -f Red "Error Updating Quick Launch Links!" $_.Exception.Message
    }
}

#Function to get each subsite in site collection
Function Get-SPOWebs($SiteURL)
{
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials

    #Get the web
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $Ctx.Load($Web.Webs)
    $Ctx.ExecuteQuery()

    #Call the function to Search & Replace Quick launch links
    Update-SPOQuickLaunchLinks -Web $web -OldURL $OldURL -NewURL $NewURL

    #Loop through each each subsite in site
    Foreach($Web in $web.Webs)
    {
        Get-SPOWebs $Web.Url
    }
}

#Variables
$AdminCenterURL = "https://crescenttech-admin.sharepoint.com"
$OldURL = "http://support.crescent.com/app"
$NewURL = "http://support.crescent.com/applications"

#Setup Credentials to connect
$Cred= Get-Credential
 
#Connect to SharePoint Online
Connect-SPOService -url $AdminCenterURL -Credential ($Cred)

#Get all Site collections
$Sites = Get-SPOSite -Limit All

#Loop through site collections
ForEach($Site in $Sites)
{
    #Call the function to get all sites in the site collection
    Get-SPOWebs($Site.URL)
}

The same script can be used for updating Top navigation links as well. Just replace: $Web.Navigation.QuickLaunch with "$Web.Navigation.TopNavigationBar"

PnP PowerShell to Update Quick Launch Links
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com"
$LinkTitle = "Support Center"

#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the Context
$Context =  Get-PnPContext

#Get the Quick Launch Navigation
$NavigationNodeCollection = Get-PnPNavigationNode -Location QuickLaunch

#Get the Link to Update
$NavigationNode = $NavigationNodeCollection | Where-Object { $_.Title -eq  $LinkTitle}
If($NavigationNode)
{
    #Update the Link URL
    $NavigationNode.Url = "https://support.crescent.com"
    $navigationNode.Update() 
    $Context.ExecuteQuery()
    Write-host "Navigation Link has been updated!"
}
SharePoint Online: Update Quick Launch Links using PowerShell SharePoint Online: Update Quick Launch Links using PowerShell Reviewed by Salaudeen Rajack on March 21, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.