Question

I am able to create a dropdown menu in SharePoint Online using the script below. The results is in the first image, is it possible to create the 2nd image using PowerShell also?

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

Image 1:

enter image description here

Image 2:

enter image description here

Edit: Attempt

enter image description here

 #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


   #Get the Navigation node "Application Support"
    $AppSupportID = Get-PnPNavigationNode -Location TopNavigationBar | Where {$_.Title -eq "Application Support"}  | Select -ExpandProperty ID

    #Add a link under "Application Support"
    Add-PnPNavigationNode -Title "Test" -Url "http://support.crescent.com/<test-url>" -Location TopNavigationBar -Parent $AppSupportID
   

enter image description here

Était-ce utile?

La solution

Here is a sample script for your reference:

#Parameters
$SiteUrl = "<SiteURL>"
$Username = "<Admin>"
$Password = "<Password>"

$ParentTitle = "1"
$ParentURL = "<Link1>"
$ChildTitle = "2"
$ChildURL = "<Link2>"
$SubChildTitle = "3"
$SubChildURL = "<Link3>"

#Connect to PnP Online
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $UserName, $(convertto-securestring $Password -asplaintext -force)
Connect-PnPOnline -Url $SiteUrl -Credential $cred

#Add a Link to Top Navigation Bar
Add-PnPNavigationNode -Title $ParentTitle -Url $ParentURL -Location TopNavigationBar

#Get the Parent Navigation node
$ParentID = Get-PnPNavigationNode -Location TopNavigationBar | Where {$_.Title -eq $ParentTitle}  | Select -ExpandProperty Id

#Add a Link under Parent link
Add-PnPNavigationNode -Title $ChildTitle -Url $ChildURL -Location TopNavigationBar -Parent $ParentID

#Get the Child Navigation node
$ChildID = (Get-PnPNavigationNode -Id $ParentID).Children

#Add a link under Child link
Add-PnPNavigationNode -Title $SubChildTitle -Url $SubChildURL -Location TopNavigationBar -Parent $ChildID.Id

This works for one child/subchild link a time.


UPDATE:

If you want to add a second child link/sub child link to the existing nodes, just repeat the process and skip creating link on the top level:

#Parameters
$SiteUrl = "<SiteURL>"
$Username = "<Admin>"
$Password = "<Password>"

$ParentTitle = "1" #Exsting Parent Link
$ChildTitle = "4"
$ChildURL = "<Link4>"
$SubChildTitle = "5"
$SubChildURL = "<Link5>"

#Connect to PnP Online
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $UserName, $(convertto-securestring $Password -asplaintext -force)
Connect-PnPOnline -Url $SiteUrl -Credential $cred

#Get the Parent Navigation node
$ParentID = Get-PnPNavigationNode -Location TopNavigationBar | Where {$_.Title -eq $ParentTitle}  | Select -ExpandProperty Id

#Add another Link under Parent link
Add-PnPNavigationNode -Title $ChildTitle -Url $ChildURL -Location TopNavigationBar -Parent $ParentID

#Get the current Child Navigation node
$ChildID = (Get-PnPNavigationNode -Id $ParentID).Children | Where {$_.Title -eq $ChildTitle}

#Add another link under Child link
Add-PnPNavigationNode -Title $SubChildTitle -Url $SubChildURL -Location TopNavigationBar -Parent $ChildID.Id

Autres conseils

Try adding one more Add-PnPNavigationNode command for "Test" navigation link like below:

#Get the Navigation node "Application Support"
$AppSupportID = Get-PnPNavigationNode -Location TopNavigationBar | Where {$_.Title -eq "Application Support"}  | Select -ExpandProperty ID

#Add a link under "Application Support"
Add-PnPNavigationNode -Title "Test" -Url "http://support.crescent.com/<test-url>" -Location TopNavigationBar -Parent $AppSupportID

Add this code after:

Add-PnPNavigationNode -Title "Application Support" -Url "http://support.crescent.com/apps" -Location TopNavigationBar -Parent $ParentID

Related Microsoft documentations:

  1. Add-PnPNavigationNode
  2. Navigation options for SharePoint Online
Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top