Create Top Navigation Link in SharePoint using JSOM

The example in this topic show how to use JSOM to Create Top Navigation Link in SharePoint

  • function CreateTopNavigationLink() {
  •     // You can optionally specify the Site URL here to get the context
  •     // If you don't specify the URL, the method will get the context of the current site
  •     // var clientContext = new SP.ClientContext("http://MyServer/sites/SiteCollection");
  •     var clientContext = new SP.ClientContext();
  •  
  •     var oWeb = clientContext.get_web();
  •  
  •     var topNavigationCollection = oWeb.get_navigation().get_topNavigationBar();
  •  
  •     // New Object "NavigationNodeCreationInformation" to create Navigation link
  •     var oNavigationNodeCreationInformation = new SP.NavigationNodeCreationInformation();
  •  
  •     oNavigationNodeCreationInformation.set_title("Node Name");
  •     oNavigationNodeCreationInformation.set_url("Node URL");
  •  
  •     topNavigationCollection.add(oNavigationNodeCreationInformation);
  •  
  •     //Load the client context and execute the batch
  •     clientContext.load(topNavigationCollection);
  •  
  •     // Execute the query to the server.
  •     clientContext.executeQueryAsync(onsuccess, onfailed);
  • }
  •  
  • function onsuccess() {
  •     console.log("success");
  • }
  •  
  • function onfailed(sender, args) {
  •     console.log('Failed' + args.get_message() + '\n' + args.get_stackTrace());
  • }

Thank you for reading this article. This code was tested in SharePoint 2013


© 2019 Code SharePoint