SharePoint Online: Add Link to Quick Launch Navigation using PowerShell

Requirement: Add Link to Quick Launch Navigation SharePoint Online using PowerShell

How to Add a Link to Quick Launch Navigation in SharePoint Online?
To add a link in quick launch navigation
  • Go to Site Settings >> Click on "Quick launch" from Look and feel
  • From Quick Launch page, You can add new heading or link by clicking relevant links.
    powershell to add link to quick launch navigation in sharepoint online
If publishing feature is enabled, You'll get "Navigation" link in site settings

SharePoint Online: PowerShell to Add Link to 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"

#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()

#Add link to Quick Launch Navigation
$NavigationNode = New-Object Microsoft.SharePoint.Client.NavigationNodeCreationInformation
$NavigationNode.Title = "Support Center"
$NavigationNode.Url = "http://support.crescent.com"
$NavigationNode.AsLastNode = $True
$Ctx.Load($QuickLaunch.Add($NavigationNode))
$Ctx.ExecuteQuery() 

Lets add some more functionality to the script, so that we can add link to any specific node:
#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"

#Add new Link in Quicklaunch Navigation
Function Add-SPONavigationNode()
{
    Param(
        [Microsoft.SharePoint.Client.NavigationNodeCollection]$Navigation,
        [parameter(Mandatory=$false)][String]$ParentNodeTitle,
        [String]$Title,
        [String]$URL
    )
    #Populate New node data
    $NavigationNode = New-Object Microsoft.SharePoint.Client.NavigationNodeCreationInformation 
    $NavigationNode.Title = $Title
    $NavigationNode.Url = $URL
    $NavigationNode.AsLastNode = $true

    #Get the Parent Node
    $ParentNode = $Navigation | Where-Object {$_.Title -eq $ParentNodeTitle}
    
    #Add New node to the navigation
    If($ParentNode -eq $null)
    {
        #Check if the Link with Title exists already
        $Node = $Navigation | Where-Object {$_.Title -eq $Title}
        If($Node -eq $Null)
        { 
            #Add Link to Root node of the Navigation
            $Ctx.Load($Navigation.Add($NavigationNode))
            $Ctx.ExecuteQuery()
            Write-Host -f Green "New Navigation Node '$Title' Added to the Navigation Root!"
        }
        Else
        {
            Write-Host -f Yellow "Navigation Node '$Title' Already Exists in Root!"
        }
    }
    else
    {
        #Get the Parent Node
        $Ctx.Load($ParentNode)
        $Ctx.Load($ParentNode.Children)
        $Ctx.ExecuteQuery()
 
        $Node = $ParentNode.Children | Where-Object {$_.Title -eq $Title}
        If($Node -eq $Null)
        { 
            #Add Link to Parent Node
            $Ctx.Load($ParentNode.Children.Add($NavigationNode))
            $Ctx.ExecuteQuery()
            Write-Host -f Green "New Navigation Link '$Title' Added to the Parent '$ParentNodeTitle'!"
        }
        Else
        {
            Write-Host -f Yellow "Navigation Node '$Title' Already Exists in Parnet Node '$ParentNodeTitle'!"
        }
    }
}
 
#Config Parameters
$SiteURL="https://crescenttech.sharepoint.com/unitedstates"

#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()

#Call the function to Add a New Node in Root of the navigation
Add-SPONavigationNode -Navigation $QuickLaunch -Title "Support Center" -URL "http://support.crescent.com"

#Call the function to Add a link in "Support Center" node of the navigation
Add-SPONavigationNode -Navigation $QuickLaunch -ParentNodeTitle "Support Center" -Title "Application Support" -URL "http://suppport.crescent.com/apps"

PnP PowerShell to Add Link to Quick Launch:
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com/us"

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PNP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
    
    #Add a Link to Quick Launch Navigation
    Add-PnPNavigationNode -Title "Support Center" -Url "http://support.crescent.com" -Location "QuickLaunch"

    #Get the Navigation node "Support Center"
    $ParentID = Get-PnPNavigationNode -Location QuickLaunch | Where {$_.Title -eq "Support Center"}  | Select -ExpandProperty ID
    #Add a link under "Support Center
    Add-PnPNavigationNode -Title "Application Support" -Url "http://support.crescent.com/apps" -Location "QuickLaunch" -Parent $ParentID
 
    Write-host "Quick Launch Links Added Successfully!" -f Green
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
SharePoint Online: Add Link to Quick Launch Navigation using PowerShell SharePoint Online: Add Link to Quick Launch Navigation using PowerShell Reviewed by Salaudeen Rajack on March 22, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.