Update Navigation programmatically for SharePoint Modern sites using SharePoint CSOM

Posted by

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.

  1. All Navigation headers need to have a Url associated to them, they cannot be just placeholders
  2. 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
  3. Urls cannot be updated on Navigation nodes which already have Urls associated to them. They will need to be re-created
  4. All provided URLs needs to link to valid page in the site for Internal Links
  5. Navigation can be nested three levels deep
  6. 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

Conclusion

Above we have got code samples of how we can update Navigation in Modern Team sites using SharePoint CSOM

2 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s