Search code examples
navigationsharepoint-2013publishing

sharepoint get navigation nodes as appear in top navigation bar programmatically


I need to get navigation nodes as appear in top navigation bar programmatically in SharePoint 2013 publishing site I did search for that and i got it with code bellow but its give me hidden item and I dint want to get hidden ones so how to get items exclude hidden items (If I use node.IsVisible in code its gives me true all time even if the item is hidden from navigation)

using (SPSite site = new SPSite(path))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPSecurity.RunWithElevatedPrivileges(delegate()
                        {

                            PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);


                            SPNavigationNodeCollection navocol = publishingWeb.Navigation.GlobalNavigationNodes;

                            foreach (SPNavigationNode node in navocol)
                            {

                                    lbl.Text = lbl.Text + " + " + node.Title + "" + node.Url + "  " + node.GetType();

                            }

                            //SPNavigationNodeCollection navCol = web.Navigation.TopNavigationBar;


                            //foreach (SPNavigationNode node in navCol)
                            //{

                                //lbl.Text = lbl.Text + " + " + node.Title + "" + node.Url;

                            //}

                        });
                    }
                }

Solution

  • Try to use web.AllProperties["__GlobalNavigationExcludes"], below code works based on my testing, referenced thread.

    string GlobalNavigationExcludes = (string)web.AllProperties["__GlobalNavigationExcludes"];
                        Hashtable ReturnExcludedUrls = new Hashtable();
    
                        if (!String.IsNullOrEmpty(GlobalNavigationExcludes))
                        {
                            string[] ExcludedGuids = GlobalNavigationExcludes.Split(';');
    
                            for (int i = 0; i < ExcludedGuids.Length - 1; i++)
                            {
                                string guid = ExcludedGuids[i];
                                Guid ExcludedGuid = new Guid(guid);
    
                                try
                                {
                                    SPWeb SubWeb = web.Webs[ExcludedGuid];
                                    ReturnExcludedUrls.Add(SubWeb.ServerRelativeUrl, "");
                                }
                                catch (Exception e1)
                                {
                                    try
                                    {
                                        SPListItem Page = web.Lists["Pages"].Items[ExcludedGuid];
                                        string ExcludedURL = String.Concat(web.ServerRelativeUrl, "/", Page.Url);
                                        ReturnExcludedUrls.Add(ExcludedURL, "");
                                    }
                                    catch (Exception e2)
                                    { }
                                }
                            }
                        }