Modification of nodes in site navigation is a very common task which is done using UI. SharePoint Client Object model also allows adding/updating/deleting nodes in site navigation.
When working with nodes in site navigation, we need to fetch nodes collection first. This can be achieved by requesting QuickLaunch property (Left Navigation) or TopNavigationBar property (Top Navigation) of Navigation class.
NavigationNodeCollection qlNavNodeColl = context.Web.Navigation.QuickLaunch;
NavigationNodeCollection topNavNodeColl = context.Web.Navigation.TopNavigationBar;
In the given samples, nodes are being modifed in left navigation only. For modification of nodes in top navigation TopNavigationBar property must be used instead of QuickLaunch.
Adding a node
private static void AddNode(string url)
{
ClientContext context = new ClientContext(url);
context.Load(context.Web);
NavigationNodeCollection qlNavNodeColl = context.Web.Navigation.QuickLaunch;
NavigationNodeCreationInformation newNavNode = new NavigationNodeCreationInformation();
newNavNode.Title = "Google";
newNavNode.Url = "https://www.google.com";
qlNavNodeColl.Add(newNavNode);
try
{
context.ExecuteQuery();
}
catch (Exception)
{
}
}
This way the node is always added as a first node in the collection. For adding node as a last node AsLastNode property of NavigationNodeCreationInformation should be set to true.
<span style="font-size: 14px;">newNavNode.AsLastNode = true;</span>
If a node is required to be added after a particular node then PreviousNode property needs to be set to the node after which our newly created node is to be added.
private static void AddNode(string url)
{
ClientContext context = new ClientContext(url);
context.Load(context.Web);
NavigationNodeCollection qlNavNodeColl = context.Web.Navigation.QuickLaunch;
IEnumerable<NavigationNode> twitterNode = context.LoadQuery(qlNavNodeColl.Where(n => n.Title == "Twitter"));
try
{
context.ExecuteQuery();
}
catch (Exception)
{
}
NavigationNodeCreationInformation newNavNode = new NavigationNodeCreationInformation();
newNavNode.Title = "Google";
newNavNode.Url = "https://www.google.com";
newNavNode.PreviousNode = twitterNode.FirstOrDefault();
qlNavNodeColl.Add(newNavNode);
try
{
context.ExecuteQuery();
}
catch (Exception)
{
}
}
Modify an existing node
private static void UpdateNode(string url)
{
ClientContext context = new ClientContext(url);
context.Load(context.Web);
NavigationNodeCollection qlNavNodeColl = context.Web.Navigation.QuickLaunch;
IEnumerable<NavigationNode> googleNode = context.LoadQuery(qlNavNodeColl.Where(n => n.Title == "Google"));
try
{
context.ExecuteQuery();
}
catch (Exception)
{
}
if (googleNode.Count() == 1)
{
NavigationNode gNode = googleNode.FirstOrDefault();
gNode.Url = "https://www.google.co.in";
gNode.Update();
try
{
context.ExecuteQuery();
}
catch (Exception)
{
}
}
}
Deleting a node
private static void DeleteNode(string url)
{
ClientContext context = new ClientContext(url);
context.Load(context.Web);
NavigationNodeCollection qlNavNodeColl = context.Web.Navigation.QuickLaunch;
IEnumerable<NavigationNode> googleNode = context.LoadQuery(qlNavNodeColl.Where(n => n.Title == "Google"));
try
{
context.ExecuteQuery();
}
catch (Exception)
{
}
if (googleNode.Count() == 1)
{
googleNode.FirstOrDefault().DeleteObject();
try
{
context.ExecuteQuery();
}
catch (Exception)
{
}
}
}