Add, Retrieve And Remove The Navigation Node Using JSOM

Introduction

 
Navigation Bar provides us with  the infrastructure to add different navigation link options in a site. We can provide the navigation links within "Top Navigation" and "Quick Launch Navigation".
 
Here, I am providing the code through which we can manipulate the navigation links both in Top navigation & Quick Launch navigation. I have added two navigation nodes; i.e., TeamSiteNavigation,TopNavigation.
 
I have used 3 buttons and their corresponding functions. Here, I am sharing the functionalities and their respective functions below.
 
For add="Add Navigation"(Function Name->addNavNode() )->
 
First, I have got the current context and web. Then I got the navigation collection of top navigation Bar. You can also get the Quick Launch by using "get_quickLaunch();"
 
Then I set properties for a new navigation node & created node as the last node in the collection.
 
For retrieve="Show Navigations Names"(Function Name->checkNavNames())->
 
Here I am retrieving the navigation collection of top navigation.
 
For remove="Remove Navigation"(FunctionName->removeNavNode())->
 
Here I am removing the navigation from the navigation collection, which I have provided in the text box.
  1. < script type = "text/javascript"  
  2. language = "javascript" >  
  3.     var navNodes = [{  
  4.         Name: "TeamSiteNavigation",  
  5.         url: "/sites/TeamSite",  
  6.         fromExternal: false  
  7.     }, {  
  8.         Name: "TopNavigation",  
  9.         url: "http://www.google.com",  
  10.         fromExternal: true  
  11.     }];  
  12. var oNavNodeColl = null;  
  13. var nodeCreationInfo = null;  
  14.   
  15. function addNavNode() {  
  16.     var ctx = new SP.ClientContext.get_current();  
  17.     if (ctx != undefined && ctx != null) {  
  18.         var oWeb = ctx.get_web();  
  19.         this.oNavNodeColl = oWeb.get_navigation().get_topNavigationBar();  
  20.         for (var i = 0; i < navNodes.length; i++) {  
  21.             var navObj = navNodes[i];  
  22.             var nodeTitle = navObj.Name;  
  23.             var navNodeUrl = navObj.url;  
  24.             var navFromExternal = navObj.fromExternal;  
  25.             this.nodeCreationInfo = new SP.NavigationNodeCreationInformation();  
  26.             nodeCreationInfo.set_title(nodeTitle);  
  27.             nodeCreationInfo.set_url(navNodeUrl);  
  28.             nodeCreationInfo.set_isExternal(navFromExternal);  
  29.             nodeCreationInfo.set_asLastNode(true);  
  30.             this.oNavNodeColl.add(nodeCreationInfo);  
  31.         }  
  32.         ctx.load(this.oNavNodeColl);  
  33.         ctx.executeQueryAsync(function() {  
  34.             alert("successfully added")  
  35.         }, function(sender, args) {  
  36.             alert('Request failed. ' + args.get_messege() + '\n' + args.get_stackTrace());  
  37.         });  
  38.     }  
  39. }  
  40.   
  41. function checkNavNames() {  
  42.     var ctx = SP.ClientContext.get_current();  
  43.     var oWeb = ctx.get_web();  
  44.     var oNavNodeColl = oWeb.get_navigation();  
  45.     var nodeColl = oNavNodeColl.get_topNavigationBar();  
  46.     ctx.load(oNavNodeColl);  
  47.     ctx.load(nodeColl, 'Include(Title,Children.Include(Title,Children))');  
  48.     ctx.executeQueryAsync(function() {  
  49.         var navNodeEnumerator = nodeColl.getEnumerator();  
  50.         var nodeNames = "";  
  51.         while (navNodeEnumerator.moveNext()) {  
  52.             var oNavNode = navNodeEnumerator.get_current();  
  53.             nodeNames = nodeNames + '\n' + oNavNode.get_title();  
  54.         }  
  55.         alert(nodeNames);  
  56.     }, function(sender, args) {  
  57.         alert('Request failed. ' + args.get_messege() + '\n' + args.get_stackTrace());  
  58.     });  
  59. }  
  60.   
  61. function removeNavNode() {  
  62.     var navName = document.getElementById("Textbox").value;  
  63.     var ctx = SP.ClientContext.get_current();  
  64.     var oWeb = ctx.get_web();  
  65.     var oNavNodeColl = oWeb.get_navigation();  
  66.     var nodeColl = oNavNodeColl.get_topNavigationBar();  
  67.     ctx.load(oNavNodeColl);  
  68.     ctx.load(nodeColl, 'Include(Title,Children.Include(Title,Children))');  
  69.     ctx.executeQueryAsync(function() {  
  70.         var navNodeEnumerator = nodeColl.getEnumerator();  
  71.         var nodeNames = "";  
  72.         while (navNodeEnumerator.moveNext()) {  
  73.             var oNavNode = navNodeEnumerator.get_current();  
  74.             nodeNames = oNavNode.get_title();  
  75.             if (nodeNames == navName) {  
  76.                 oNavNode.deleteObject();  
  77.                 ctx.executeQueryAsync(function() {  
  78.                     alert("successfully deleted");  
  79.                 }, function(sender, args) {  
  80.                     alert('Request failed. ' + args.get_messege() + '\n' + args.get_stackTrace());  
  81.                 });  
  82.             }  
  83.         }  
  84.     }, function(sender, args) {  
  85.         alert('Request failed. ' + args.get_messege() + '\n' + args.get_stackTrace());  
  86.     });  
  87. } < /script>   
  88. < input id = "addButton"  
  89.     type = "button"  
  90.     value = "Add Navigation"  
  91.     onclick = "addNavNode()" / >< br / >< div style = "marginTop:20px;" > & nbsp < /div>< input id = "checkButton"  
  92.     type = "button"  
  93.     value = "Show Navigations Names"  
  94.     onclick = "checkNavNames()" / >< br / >< div style = "marginTop:20px;" > & nbsp < /div>< label > Enter navigation name to delete < /label>< input id = "Textbox"  
  95.     type = "text" / >< input id = "removeButton"  
  96.     type = "button"  
  97.     value = "Remove Navigation"  
  98. onclick = "removeNavNode()" >  
Follow the below instructions and refer to the corresponding image to perform the Operation,
 
Click on Add Navigation,
 
Add, Retrieve And Remove The Navigation Node Using JSOM 
 
After adding the node,
 
Add, Retrieve And Remove The Navigation Node Using JSOM
 
Click on Show Navigations Names,
 
Add, Retrieve And Remove The Navigation Node Using JSOM
 
Now we can check Navigation name in the alertbox,
 
Add, Retrieve And Remove The Navigation Node Using JSOM
 
Give the navigation node which you want to delete.
 
Add, Retrieve And Remove The Navigation Node Using JSOM
 
I've successfully deleted the navigation node.
 
Add, Retrieve And Remove The Navigation Node Using JSOM