Question

I'm trying to create links under 'Global Navigation' and checked "Show All pages" option in SharePoint Online. It is not creating links in Global navigation and it is not throwing any exception also. I have checked that in AreaNavigationSettings.aspx links are not added. We have created master page using design manager there i have used 'Top navigation' snippet.

        Web web = context.Web;
        context.Load(web);
        NavigationNodeCollection topNavNodeColl = web.Navigation.TopNavigationBar;
        //Create a new node
        NavigationNodeCreationInformation newNavNode = new NavigationNodeCreationInformation();
        newNavNode.Title = "sample title";
        newNavNode.Url = "site url"; 
        newNavNode.AsLastNode = false; // I have tried with 'true' also
        topNavNodeColl.Add(newNavNode);
        context.Load(topNavNodeColl);
        context.ExecuteQuery();
Was it helpful?

Solution 3

There is no issue with the code. Issue is with the Site collection (Test-dev).I have tried the same code on a different site collection (Test-stag) it is working fine. Now i have to create links instead of headings.

OTHER TIPS

Modify your code as mentioned below.

We need to initialize the Navigation node collection to include the latest added node :

// previous code 
NavigationNodeCollection topNavNodeColl = context.Web.Navigation.TopNavigationBar;
NavigationNodeCreationInformation newNavNode = new NavigationNodeCreationInformation();
newNavNode.Title = "sample title";
newNavNode.Url = "site url"; 
newNavNode.AsLastNode = false;
context.Load(topNavNodeColl.Add(newNavNode));  // changed here 
context.ExecuteQuery();

Maybe site Navigation settings has not been enabled at site collection level.

To enable please Navigate to Site Settings -> Site Collection Navigation under Site Collection Administrator -> Enable Enable Navigation -> Click OK .

As soon as you will enable it, you should be able to see all the navigation terms that you are seeing while looping through "topNavNodeColl" collection.

I hope this helps !!!

You can try to create a navigation node using the PnP PowerShell library.

Add-PnPNavigationNode -Title "sample title" -Url "site url" -Location "TopNavigationBar"

More at: https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/add-pnpnavigationnode?view=sharepoint-ps

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top