kwizcom banner advertisement

SharePoint Online: Add Top Navigation Link using PowerShell CSOM

Requirement: Add link to top navigation in SharePoint Online using PowerShell

PowerShell to Add Link to Top Navigation in SharePoint Online:
Here is the PowerShell to add link to global navigation of the SharePoint Online site.
#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 Top Navigation
Function Add-SPOTopNavigationLink()
{
    Param(
        [String]$SiteURL,
        [parameter(Mandatory=$false)][String]$ParentNodeTitle,
        [String]$Title,
        [String]$URL
    )

    #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 Top Navigation of the web
    $TopNavigationBar = $Ctx.Web.Navigation.TopNavigationBar
    $Ctx.load($TopNavigationBar)
    $Ctx.ExecuteQuery()

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

#Call the function to Add a New Link in Root of the navigation
Add-SPOTopNavigationLink -SiteURL $SiteURL -Title "Support Center" -URL "http://support.crescent.com"

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

Result of the script:
sharepoint online add link to top navigation using powershell

SharePoint Online: PnP PowerShell to Add Link in Global Navigation Menu Bar
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com"

#Get Credentials to connect
$Cred = Get-Credential

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

    #Get the Navigation node "Support Center"
    $ParentID = Get-PnPNavigationNode -Location TopNavigationBar | 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 TopNavigationBar -Parent $ParentID
 
    Write-host "Quick Launch Links Added Successfully!" -f Green
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
SharePoint Online: Add Top Navigation Link using PowerShell CSOM SharePoint Online: Add Top Navigation Link using PowerShell CSOM Reviewed by Salaudeen Rajack on March 23, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.