Delete Top Navigation Link in SharePoint using CSOM

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

  • using Microsoft.SharePoint.Client;
  • using System.Linq;
  •  
  • using (ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection"))
  • {
  • NavigationNodeCollection topNavigationNodes = null;
  •  
  • // Option 1: This will get quick launch collection from parent web
  • // This option will work only if the method is running from sub - site otherwise parent ID will be Null
  • clientcontext.Load(clientcontext.Web.ParentWeb, p => p.Id);
  •  
  • clientcontext.ExecuteQuery();
  • topNavigationNodes = clientcontext.Site.OpenWebById(clientcontext.Web.ParentWeb.Id).Navigation.TopNavigationBar;
  •  
  • // Option 2: This will get quick launch collection from current site
  • topNavigationNodes = clientcontext.Web.Navigation.TopNavigationBar;
  •  
  • clientcontext.Load(topNavigationNodes);
  • clientcontext.ExecuteQuery();
  •  
  • NavigationNode node = (from navigationNode in topNavigationNodes where "Node Name" == navigationNode.Title select navigationNode).FirstOrDefault();
  •  
  • // Condition to check the existence of the navigation node.
  • if (node != null)
  • {
  • node.DeleteObject();
  • clientcontext.ExecuteQuery();
  • }
  • }

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


© 2019 Code SharePoint