SharePoint 2013 - Add Top Navigation Link To A Site Using REST API

Introduction

In this article, we will look at how to add links to the Top Navigation Bar in a SharePoint site using REST API. I will demonstrate this by using a little jQuery and REST API to add a link to the Top Navigation which makes it a custom top navigation link since we are using some code to achieve it.

Prerequisites

REST API TopNavigationbar EndPoint to use in Add_ins.

/_api/web/navigation/TopNavigationbar

Scenario

Now, I have created a site which has just the site name for the top navigation which navigates to the Home page by default.

SharePoint

So, let’s say we are adding a custom navigation link for the Top Navigation, i.e., ‘’AnotherPage”.

Implementation

We will achieve this by adding a link to the top navigation bar on a button click. Please follow the step by step procedure for the same.

Step 1

Go to your SharePoint 2013 site.

Step 2

Now, you are possibly on the home page. Go to Site Actions > Edit Page.

Once the page is in "Edit" mode, add a Script Editor Web Part to the page. For that, click “Web Part” option under the "Insert" tab on the ribbon. In the Categories section, click “Media and Content”, select the “Script Editor” Web Part in the adjacent window, and click "Add" button.

Step 3

Once the Web Part is inserted into the page, edit the Web Part and then "EDIT SNIPPET". Insert the HTML and/or JavaScript, as shown below.

  1. <input type="button" value="Add Top Navigation Link" id="createTopNav"></input>  
  2. <script type="text/javascript" src="../../SiteAssets/1.11.2.jquery.js"></script>  
  3. <script type="text/javascript">  
  4.     $(function() {  
  5.         $("#createTopNav").click(function() {  
  6.             CreateTopNavigationLink()  
  7.         });  
  8.     });  
  9.   
  10.     function CreateTopNavigationLink() {  
  11.         var siteUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/navigation/TopNavigationbar";  
  12.         var headers = {  
  13.             "accept""application/json;odata=verbose",  
  14.             "content-Type""application/json;odata=verbose",  
  15.             "X-RequestDigest": jQuery("#__REQUESTDIGEST").val()  
  16.         }  
  17.         var call = jQuery.ajax({  
  18.             url: siteUrl,  
  19.             type: "POST",  
  20.             data: JSON.stringify({  
  21.                 "__metadata": {  
  22.                     type: "SP.NavigationNode"  
  23.                 },  
  24.                 'IsExternal'true,  
  25.                 'Title'"AnotherPage",  
  26.                 'Url'"http://www.anotherpage.com"  
  27.             }),  
  28.             headers: headers  
  29.         });  
  30.         call.done(successHandler);  
  31.         call.fail(failureHandler);  
  32.     });  
  33.   
  34.     function successHandler(data, textStatus, jqXHR) {  
  35.         alert("Navigation created Successully");  
  36.     }  
  37.   
  38.     function failureHandler(errorMessage) {  
  39.         alert("Request Failed: unable to Navigation: " + JSON.stringify(errorMessage));  
  40.     }  
  41. </script>  

Step 4

After inserting the HTML and Script in the Script Editor, you will see a button on the page, viz. “Add Top Navigation Link”. Click the link to add the link “AnotherPage” Top Navigation Bar, as shown in the screenshot below.

SharePoint

Hope this article was helpful.