SharePoint 2013 Add Top Navigation Link to a Site Using REST API

SharePoint 2013 Add Top Navigation Link to a Site Using REST API
InfoPath alternatives for form designing SharePoint

In this article, we will look for how to add links to the Top Navigation bar on a SharePoint site using REST API. I will demonstrate this by using a little JQuery and REST API to add a link in 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

Read some SharePoint 2013 tutorials below:

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 2013 Add Top Navigation Link to a Site Using REST API
SharePoint 2013 Add Top Navigation Link to a Site Using REST API

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:
Open your SharePoint 2013 site. 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.

To add a Script Editor Web Part, 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-2:
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.

Add Top Navigation Link to a Site Using REST API in SharePoint 2013:



<input type=”button” value=”Add Top Navigation Link” id=”createTopNav”></input>

<script type=”text/javascript” src=”../../SiteAssets/1.11.2.jquery.js”></script>

<script type=”text/javascript”>
$(function (){
$(“#createTopNav”).click(function () { CreateTopNavigationLink() });
});
function CreateTopNavigationLink() {
var siteUrl = _spPageContextInfo.webAbsoluteUrl + “/_api/web/navigation/TopNavigationbar”;
var headers = {
“accept”: “application/json;odata=verbose”,
“content-Type”: “application/json;odata=verbose”,
“X-RequestDigest”: jQuery(“#__REQUESTDIGEST”).val()
}
var call = jQuery.ajax({
url: siteUrl,
type: “POST”,
data: JSON.stringify({
“__metadata”: { type: “SP.NavigationNode” },
‘IsExternal’: true,
‘Title’: “AnotherPage”,
‘Url’: “http://www.anotherpage.com”
}),
headers: headers
});
call.done(successHandler);
call.fail(failureHandler);
});

function successHandler(data, textStatus, jqXHR) {
alert(“Navigation created Successully”);
}
function failureHandler(errorMessage) {
alert(“Request Failed: unable to Navigation: ” + JSON.stringify(errorMessage));
}
</script>

Step-3:
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 online Add Top Navigation Link to a Site Using REST API
SharePoint online Add Top Navigation Link to a Site Using REST API

Hope this Article was helpful.

Similar SharePoint 2013 Tutorials


About Akash Kumhar

I am Akash Kumhar a SharePoint Consultant.

View all posts by Akash Kumhar →

Leave a Reply