none
Read Quick Launch items programmatically RRS feed

  • Question

  • This is the code I'm using:

    using (SPSite site = new SPSite(siteUrl))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPNavigationNodeCollection _quickLaunchNav = web.Navigation.QuickLaunch;

    }

    }

    The problem:

    in the _quickLaunchNav variable, I get 63 items, but my Quick Launch has on 12 on GUI.

    Same thing is when I go to any subsite, I get too many navigation items. How can I filter them to get the exact number of quick launch items as defined on the GUI?

    Edit: If it is important, it is a publishing site.

    Edit2:

    When using this:

    var pubWeb = PublishingWeb.GetPublishingWeb(web);                   
    pubWeb.Navigation.CurrentNavigationNodes

    I get 62 items, but when I use this:

    var pubWeb = PublishingWeb.GetPublishingWeb(web);                 
    pubWeb.Navigation.GlobalNavigationNodes

    I get 50 items.

    How can I exclude each other and get to my 12 items??

    Friday, October 18, 2019 9:11 PM

All replies

  • Hi Mario,

    Could you please try this code-

     using (SPWeb xWeb = siteCollection.OpenWeb())
                    {
                        //Add the library to quick launch
                        SPNavigationNodeCollection nodes = xWeb.Navigation.QuickLaunch;
                        for (int i = 0; i < nodes.Count; i++)
                        {
                            if (nodes[i].IsVisible == true)
                            {
                 
                                
                            }
                        }
    
                        Console.ReadKey();
                    }


    K Mohit

    Sunday, October 20, 2019 3:30 PM
  • Hi Mario,

    Please check the code below.

    var siteUrl = "http://sp2013/sites/team";                         
    using (SPSite site = new SPSite(siteUrl))
    {
    	using (SPWeb web = site.OpenWeb())
    	{
    		foreach (SPNavigationNode nav in web.Navigation.QuickLaunch)
    		{
    			if (nav.IsVisible)
    			{
    				Console.WriteLine(nav.Title);
    				foreach (SPNavigationNode cnav in nav.Children)
    				{
    					if (nav.IsVisible)
    					{
    						Console.WriteLine("  "+cnav.Title);
    					}
    				}
    			}
    		}                  
    	}
    }

    If the current navigation are not include the sub sites, please using the code below.

    var siteUrl = "http://sp2013/sites/team";                         
    using (SPSite site = new SPSite(siteUrl))
    {
    	using (SPWeb web = site.OpenWeb())
    	{
    		foreach (SPNavigationNode nav in web.Navigation.QuickLaunch)
    		{
    			if (nav.IsVisible&&nav.TargetParentObjectType.ToString()!="Web")
    			{
    				Console.WriteLine(nav.Title);
    				foreach (SPNavigationNode cnav in nav.Children)
    				{
    					if (nav.IsVisible)
    					{
    						Console.WriteLine("  "+cnav.Title);
    					}
    				}
    			}
    		}                  
    	}
    }

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.


    Monday, October 21, 2019 8:33 AM
    Moderator
  • No, the result is always the same; 63 items.
    Monday, October 21, 2019 9:29 AM
  • Hi Mario,

    Please provide more information about your navigation settings(go to Site settings->Look and Feel->Navigation).

    And provide some screenshots for further research.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Tuesday, October 22, 2019 9:52 AM
    Moderator
  • Hi Mario,

    Would you please provide us with an update on the status of your issue?

    Best Regards,
    Dennis

    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Monday, October 28, 2019 8:24 AM
    Moderator