Create Top Navigation Nodes In SharePoint Using JavaScript Object Model

SharePoint top navigation bar helps users to navigate to other sites. Top Navigation is also referred to as Global Navigation because it remains the same throughout the site collection. However, subsites can be configured in a way so that the top navigation does not come up in the top bar. We will have to enable publishing feature to work on the top navigation bar as it comes bundled with it.


Top navigation can be of two types.

  • Structural Navigation
  • Metadata Navigation

Structural navigation is a navigation based on the site structure and the links will redirect to other sub sites, whereas, Metadata Navigation was introduced in SharePoint 2013 and is based on metadata terms defined in the taxonomy store. We can assign redirection URLs to the terms during their creation which can later be used in the top navigation.

Top Navigation can be configured by going to the Site Settings -> Navigation location.


In this article, we will see how we can add new navigation nodes to the top navigation bar using JavaScript Object Model.

Internal Implementation

  • Add reference to jQuery file.
    1. <script language="javascript" type="text/javascript" src="//"></script>  
    2. <script language="javascript" type="text/javascript">  
  • Within Document ready function, call SP.SOD.executeFunc so as to load the on demand script SP.js . Call the main starting point function, say: addTopNavigation
    1. SP.SOD.executeFunc('sp.js''SP.ClientContext', addTopNavigation);  
  • Instantiate client context and get the web instance.
    1. //Get client context and web object  
    2. var clientContext = new SP.ClientContext();  
    3. var oWeb = clientContext.get_web();  
  • Create top navigation node object using ‘NavigationNodeCreationInformation’ object
    1. oTopNavigationColl =oWeb.get_navigation().get_topNavigationBar();  
    2. //Add a new top navigation object using NavigationNodeCreationInformation object   
    3. var oNavigation = new SP.NavigationNodeCreationInformation();   
    4. oNavigation.set_title("Employee Master List");   
    5. oNavigation.set_url("/sites/playground/Lists/Employees/AllItems.aspx");   
    6. oTopNavigationColl.add(oNavigation);   
  • Load the client context and execute the batch.
    1. clientContext.load(oQuickLaunchColl);  
    2. clientContext.executeQueryAsync(QuerySuccess, QueryFailure);  
    Full Code

    The full code to add Top Navigation items using JavaScript Object Model is, as shown below:
    1. <script language="javascript" type="text/javascript" src="//"></script>  
    2. <script language="javascript" type="text/javascript">  
    3.     $(document).ready(function() {  
    4.         SP.SOD.executeFunc('sp.js''SP.ClientContext', addTopNavigation);  
    5.     });  
    7.     function addTopNavigation() {  
    8.         //Get the client context,web and top navigation object   
    9.         var clientContext = new SP.ClientContext();  
    10.         var oWeb = clientContext.get_web();  
    11.         oTopNavigationColl = oWeb.get_navigation().get_topNavigationBar();  
    12.         //Add a new top navigation object using NavigationNodeCreationInformation object   
    13.         var oNavigation = new SP.NavigationNodeCreationInformation();  
    14.         oNavigation.set_title("Employee Master List");  
    15.         oNavigation.set_url("/sites/playground/Lists/Employees/AllItems.aspx");  
    16.         oTopNavigationColl.add(oNavigation);  
    17.         //Load the client context and execute the batch   
    18.         clientContext.load(oTopNavigationColl);  
    19.         clientContext.executeQueryAsync(QuerySuccess, QueryFailure);  
    20.     }  
    22.     function QuerySuccess() {  
    23.         //Get the node count and loop through the collection   
    24.         var itemCount = oTopNavigationColl.get_count();  
    25.         console.log("Top Navigation Details are:");  
    26.         for (var i = 0; i < itemCount; i++) {  
    27.             var title = oTopNavigationColl.get_item(i).get_title();  
    28.             console.log(title);  
    29.         }  
    30.     }  
    32.     function QueryFailure(sender, args) {  
    33.         console.log('Request failed' + args.get_message());  
    34.     }  
    35. </script>  

Let’s see how we can implement it in SharePoint. Save the above scripts onto a text file and upload it to Site Assets library.

SharePoint Implementation

  • Go to the edit settings of the SharePoint page and click on Web part from the Insert tab,

    Web part

  • Add Content Editor Web part.

    Content Editor

  • Click on Edit Web art from Content Edit Web part. Assign the URL of the script text file and click on Apply.

    Content Editor

    Click on Apply. We can see the new top navigation item added to the top link bar.



Thus, we have seen how we can modify the top navigation bar programmatically using JavaScript Object Model .This has been tried and tested out in SharePoint 2016 and Office 365.

Up Next
    Ebook Download
    View all
    View all