2

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();
2

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();
  • Thanks for your response Gautham. I have tried your code but still the same issue. The problem is if i loop through "topNavNodeColl" collection i'm getting all the nodes thathave added. But it is not added to the "Global navigation" in the SharePoint Site as well as in "navigation" settings. – Vijay Aug 1 '18 at 11:34
  • can you check with updated code ? Also, i tested the code above and works for me, check this image. Also, check if you are checking the correct web – Gautam Sheth Aug 1 '18 at 11:41
  • Yes, I have tried the updated code and checking the correct web. The option in Navigation settings is "Show pages". I'm using Page URL for newNavNode.Url and the account i'm using is 'Site Coll Admin' – Vijay Aug 1 '18 at 11:49
  • if you scroll down, you will find the Structural Navigation : Editing and Sorting section, where you will find your node like this. Also, do you find the node in this section ? Also, check the HTML DOM, it could be that the nodes might be hidden via CSS ? – Gautam Sheth Aug 1 '18 at 12:01
  • Yes, I'm looking in to the same option there are no nodes are added. There is one existing page present in the Global navigation so it can't be CSS issue. I have chceked HTML DOM also only one <td> got added – Vijay Aug 1 '18 at 12:16
1

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 !!!

  • Thanks for your response. But the problem here is programmatically created links are not being visible in the global navigation. If i create manually links are getting added and visible in the navigation – Vijay Aug 2 '18 at 9:07
  • Yes that's right. But have you tried by enabling navigation from Site Collection Navigation under Site Collection Administration section on bottom. Same things were happening for me until the above mentioned config as disabled. – Ravi Aug 2 '18 at 9:13
  • Yes, "Enable navigation" is checked and enabled. – Vijay Aug 2 '18 at 9:21
0

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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.