Add, Get And Delete Quick Launch Navigation Using C#

In this blog, we have discussed adding, retrieving, and deleting the SharePoint online quick launch navigation menu using the C# Server Object model. Follow the below coding to get the result.
 

Add new term in the quick launch

 
Add the below code in your Program.cs.
  1. namespace GetNavigationNode {  
  2.     class Program {  
  3.         static void Main(string[] args) {  
  4.             string userName = "[email protected]";  
  5.             string password = "********";  
  6.             string siteUrl = "https:// tenantname.sharepoint.com/sites/AssociateSite";  
  7.             SecureString securePassword = new SecureString();  
  8.             foreach(char c in password) {  
  9.                 secure Password.AppendChar(c);  
  10.             }  
  11.             var credentials = new SharePointOnlineCredentials(userName, securePassword);  
  12.             using(ClientContext clientContext = new ClientContext(siteUrl)) {  
  13.                 client Context.Credentials = credentials;  
  14.                 Navigation NodeCollection qlNavNodeColl = clientContext.Web.Navigation.QuickLaunch;  
  15.                 client Context.Load(clientContext.Web);  
  16.                 clientContext.Load(qlNavNodeColl);  
  17.                 clientContext.ExecuteQuery();  
  18.                 NavigationNodeCreationInformation newNode = new NavigationNodeCreationInformation();  
  19.                 newNode.Title = "Search";  
  20.                 newNode.Url = "https://www.google.com";  
  21.                 qlNavNodeColl.Add(newNode);  
  22.                 clientContext.ExecuteQuery();  
  23.             }  
  24.         }  
  25.     }  
  26. }  
After running the code,
 
Add, Get And Delete Quick Launch Navigation Using C#
 

Get Navigation node

 
Add the below code to get all the term from quicklunch.
  1. using(ClientContext clientContext = new ClientContext(siteUrl)) {  
  2.     clientContext.Credentials = credentials;  
  3.     NavigationNodeCollection qlNavNodeColl = clientContext.Web.Navigation.QuickLaunch;  
  4.     clientContext.Load(clientContext.Web);  
  5.     clientContext.Load(qlNavNodeColl);  
  6.     clientContext.ExecuteQuery();  
  7.     foreach(NavigationNode navToDelete in qlNavNodeColl) {  
  8.         var navNodeTitle = navToDelete.Title;  
  9.         Console.WriteLine("Navigation Node is : " + navNodeTitle);  
  10.     }  
  11. }  
Add, Get And Delete Quick Launch Navigation Using C# 
 

Delete Navigation Node

 
Add below code to remove the quick launch term
  1. string userName = "[email protected]";  
  2. string password = "********";  
  3. string siteUrl = "https:// tenantname.sharepoint.com/sites/AssociateSite";  
  4. SecureString securePassword = new SecureString();  
  5. foreach(char c in password) {  
  6.     securePassword.AppendChar(c);  
  7. }  
  8. var credentials = new SharePointOnlineCredentials(userName, securePassword);  
  9. using(ClientContext clientContext = new ClientContext(siteUrl)) {  
  10.     clientContext.Credentials = credentials;  
  11.     NavigationNodeCollection qlNavNodeColl = clientContext.Web.Navigation.QuickLaunch;  
  12.     clientContext.Load(clientContext.Web);  
  13.     clientContext.Load(qlNavNodeColl);  
  14.     clientContext.ExecuteQuery();  
  15.     foreach(NavigationNode navToDelete in qlNavNodeColl) {  
  16.         var title = navToDelete.Title;  
  17.         if (title == "Search") {  
  18.             navToDelete.DeleteObject();  
  19.             clientContext.ExecuteQuery();  
  20.         }  
  21.     }  
  22. }  
  23. }  
After running the code,
 
Add, Get And Delete Quick Launch Navigation Using C# 
 

Conclusion

 
From the above example of the code, we can conclude that, the CRUD operation of a quick launch navigation node in SharePoint Online using c# is very effective as it reduces the amount of time. You can also use this code for SharePoint 2019 and 2016.