Articles about European Sharepoint Hosting Service
SharePoint Hosting – Add, Retrieve And Remove The Navigation Node Using JSOM
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.
1 |
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.
1 |
For retrieve="Show Navigations Names"(Function Name->checkNavNames())-> |
Here I am retrieving the navigation collection of top navigation.
1 |
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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
< script type = "text/javascript" language = "javascript" > var navNodes = [{ Name: "TeamSiteNavigation", url: "/sites/TeamSite", fromExternal: false }, { Name: "TopNavigation", url: "http://www.google.com", fromExternal: true }]; var oNavNodeColl = null; var nodeCreationInfo = null; function addNavNode() { var ctx = new SP.ClientContext.get_current(); if (ctx != undefined && ctx != null) { var oWeb = ctx.get_web(); this.oNavNodeColl = oWeb.get_navigation().get_topNavigationBar(); for (var i = 0; i < navNodes.length; i++) { var navObj = navNodes[i]; var nodeTitle = navObj.Name; var navNodeUrl = navObj.url; var navFromExternal = navObj.fromExternal; this.nodeCreationInfo = new SP.NavigationNodeCreationInformation(); nodeCreationInfo.set_title(nodeTitle); nodeCreationInfo.set_url(navNodeUrl); nodeCreationInfo.set_isExternal(navFromExternal); nodeCreationInfo.set_asLastNode(true); this.oNavNodeColl.add(nodeCreationInfo); } ctx.load(this.oNavNodeColl); ctx.executeQueryAsync(function() { alert("successfully added") }, function(sender, args) { alert('Request failed. ' + args.get_messege() + '\n' + args.get_stackTrace()); }); } } function checkNavNames() { var ctx = SP.ClientContext.get_current(); var oWeb = ctx.get_web(); var oNavNodeColl = oWeb.get_navigation(); var nodeColl = oNavNodeColl.get_topNavigationBar(); ctx.load(oNavNodeColl); ctx.load(nodeColl, 'Include(Title,Children.Include(Title,Children))'); ctx.executeQueryAsync(function() { var navNodeEnumerator = nodeColl.getEnumerator(); var nodeNames = ""; while (navNodeEnumerator.moveNext()) { var oNavNode = navNodeEnumerator.get_current(); nodeNames = nodeNames + '\n' + oNavNode.get_title(); } alert(nodeNames); }, function(sender, args) { alert('Request failed. ' + args.get_messege() + '\n' + args.get_stackTrace()); }); } function removeNavNode() { var navName = document.getElementById("Textbox").value; var ctx = SP.ClientContext.get_current(); var oWeb = ctx.get_web(); var oNavNodeColl = oWeb.get_navigation(); var nodeColl = oNavNodeColl.get_topNavigationBar(); ctx.load(oNavNodeColl); ctx.load(nodeColl, 'Include(Title,Children.Include(Title,Children))'); ctx.executeQueryAsync(function() { var navNodeEnumerator = nodeColl.getEnumerator(); var nodeNames = ""; while (navNodeEnumerator.moveNext()) { var oNavNode = navNodeEnumerator.get_current(); nodeNames = oNavNode.get_title(); if (nodeNames == navName) { oNavNode.deleteObject(); ctx.executeQueryAsync(function() { alert("successfully deleted"); }, function(sender, args) { alert('Request failed. ' + args.get_messege() + '\n' + args.get_stackTrace()); }); } } }, function(sender, args) { alert('Request failed. ' + args.get_messege() + '\n' + args.get_stackTrace()); }); } < /script> < input id = "addButton" type = "button" value = "Add Navigation" onclick = "addNavNode()" / >< br / >< div style = "marginTop:20px;" > & nbsp < /div>< input id = "checkButton" type = "button" value = "Show Navigations Names" onclick = "checkNavNames()" / >< br / >< div style = "marginTop:20px;" > & nbsp < /div>< label > Enter navigation name to delete < /label>< input id = "Textbox" type = "text" / >< input id = "removeButton" type = "button" value = "Remove Navigation" onclick = "removeNavNode()" > |
Follow the below instructions and refer to the corresponding image to perform the Operation,
Click on Add Navigation,
Print article | This entry was posted by Peter on December 23, 2020 at 4:48 am, and is filed under European SharePoint 2013 Hosting. Follow any responses to this post through RSS 2.0. Both comments and pings are currently closed. |