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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Create a new Navigation node using NavigationNodeCreationInformation class with new URL. Add the navigation node after the parent node. Delete the existing node.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Yes, JSOM are just libraries but the challenge with JSOM is that it depends on helpers files that needs to be loaded first. You have to use enuserScriptLoaded which doesn’t work well with SPFx lifecycle. Will just to use pnpjs – https://pnp.github.io/pnpjs/sp/docs/
Is there a way to do this in SPFx using JSOM?
LikeLike
Yes, JSOM are just libraries but the challenge with JSOM is that it depends on helpers files that needs to be loaded first. You have to use enuserScriptLoaded which doesn’t work well with SPFx lifecycle. Will just to use pnpjs – https://pnp.github.io/pnpjs/sp/docs/
LikeLike
Hi Asish,
I want to create custom Left navigation from list(lookup column) in modern site. can you help me
Like,
India
Chennai
Bangalore
USA
California
Europe
Germany
Berlin
LikeLike
India is a header and chennai , bangalore is subheader
LikeLike
I want to create left navigation from list (lookup column) in sharepoint modern template.. please help to implement.
India
-Chennai
-Bangalore
India is a header and chennai and bangalore is subheader…
LikeLike
Yeah sure, just apply the same above logic but then read the Title and Urls from the list in the above code. Let me know if this makes sense.
LikeLike