Delete Top Navigation Link in SharePoint using JSOM

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

  • var clientContext, topNavigationCollection;
  •  
  • function DeleteTopNavigationLink() {
  •     // 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");
  •     clientContext = new SP.ClientContext();
  •  
  •     var oWeb = clientContext.get_web();
  •  
  •     // Get Top Navigation node collection
  •     topNavigationCollection = oWeb.get_navigation().get_topNavigationBar();
  •  
  •     //Load the client context and execute the batch
  •     clientContext.load(topNavigationCollection);
  •  
  •     // Execute the query to the server.
  •     clientContext.executeQueryAsync(onsuccess, onfailed);
  • }
  •  
  • function onsuccess() {
  •  
  •     //Get the item count
  •     var itemCount = topNavigationCollection.get_count();
  •  
  •     for (var i = 0; i < itemCount; i++) {
  •         var title = topNavigationCollection.get_item(i).get_title();
  •  
  •         // Enter the node name you want to delete
  •         if (title == "New Link") {
  •             // Delete the top navigation node
  •             topNavigationCollection.get_item(i).deleteObject();
  •  
  •             // Break the for loop
  •             break;
  •         }
  •     }
  •  
  •     //Execute the batch
  •     clientContext.executeQueryAsync(FinalQuerySuccess, FinalQueryFailure);
  • }
  •  
  • function onfailed(sender, args) {
  •     console.log('Failed' + args.get_message() + '\n' + args.get_stackTrace());
  • }
  •  
  • function FinalQuerySuccess(sender, args) {
  •     console.log("success");
  • }
  •  
  • function FinalQueryFailure(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