0

I need some assistance with my PowerShell script. Currently I am able to add a drop down menu to my page, but I am trying to add some subfolders to "Support Center". Also if I wanted to add my drop down to multiple $SiteURL, how do I create it to loop for that?

I have pasted my current code below that adds a drop down in one site URL.

Thank you for any assistance!

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

    $user = "user"
    $file = "location"
    #Create new credential object and get encrypted password from file 
    $cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user, (Get-Content $file | ConvertTo-SecureString)
    $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://test.sharepoint.com/sites/TestTopLinkBar"
 
#Call the function to Add a New Link in Root of the navigation
Add-SPOTopNavigationLink -SiteURL $SiteURL -Title "TESTING - HOME" -URL "https://yahoo.com"
 
#Call the function to Add a link in "Support Center" node of the navigation
Add-SPOTopNavigationLink -SiteURL $SiteURL -ParentNodeTitle "TESTING - HOME" -Title "Support Center" -URL "https://google.com"

enter image description here

2 Answers 2

1

I think for your requirement, you can go with the megamenu and hub site configuration.

What is Megamenu Navigation?

The much-awaited megamenu navigation was recently launched by Microsoft (February & March 2019). It addresses many issues that users have been facing over the last several years in the traditional drop-down navigation approach. One of the major issues of drop-down based navigation is, if the navigation item lists are more, most of the actual content of the site will be hidden or shadowed by the drop-down menu. This is how the user gets frustrated and it leads to poor user adoption to the site. And also in the drop-down-based approach, we cannot have multi-levels of nested items.

For details refer to the below articles:

How to create navigation in modern SharePoint Online

How to create hub site in SharePoint Online using PnP PowerShell?

1

You can write the sites in a CSV file and import it using

Import-CSV -Path C:\SiteURLs.csv

And the loop should look like:

Import-Csv "C:\SiteURLs.csv" | Foreach-Object {
    Add-SPOTopNavigationLink -SiteURL $_ -Title "Support Center" -URL "/"
    Add-SPOTopNavigationLink -SiteURL $_ -ParentNodeTitle "Support Center" -Title "Application Support" -URL "/"
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.