Updating Navigation programmatically for SharePoint sites is challenging. This has become even more challenging with Modern SharePoint Team sites.
Some of the constraints are below.
- All Navigation headers need to have a Url associated to them, they cannot be just placeholders
- External Site Urls and Internal Site Urls are identified explicitly in the Navigation and it is not possible to change them without deleting and recreating
- Urls cannot be updated on Navigation nodes which already have Urls associated to them. They will need to be re-created
- All provided URLs needs to link to valid page in the site for Internal Links
- Navigation can be nested three levels deep
- Deletion of Navigation nodes needs extra steps and cannot delete
In this blog, we will look at steps on how we can update the Navigation using SharePoint CSOM. Few of the below steps are not possible through PnP PowerShell so CSOM is the best bet on this.
Traversing Navigation nodes
Traverse the navigation items using nested foreach statements to find the exact node to do the changes.
Update URL of Navigation node
Create a new Navigation node using NavigationNodeCreationInformation class with new URL. Add the navigation node after the parent node. Delete the existing node.
Delete an Existing node
Note: Deletion cannot be done while traversing the nested Navigation node Collection.
Get the Navigation ID using the below code, put into an Array for deletion and then delete it using Web Object
Set Navigation Header
This can be set expliclity through PnPPowerShell using below. For CSOM add it to the top level of the Node collection
Add-PnPNavigationNode -Location QuickLaunch -Title "Test Nav" -Header $true
Above we have got code samples of how we can update Navigation in Modern Team sites using SharePoint CSOM