How to access Sharepoint SPNavigationNode.QuickLaunch?

I have a website as follows:

- SiteA
---- Subsite1
---- Subsite2

Now when I try to access the QuickLaunch property, it is always empty, for example

SPNavigation nav = spWeb.Navigation;
if (nav.QuickLaunch.Count == 0)
{
      // ALWAYS TRUE
}

However, if I go to SiteA's Navigation Settings (via the user interface) and reorder any site in the list, only then QuickLanuch will become available. (The rest of the settings are left by default)

Can anyone explain this behavior? I really need access to QuickLaunch items.

thanks

+3
source share
3 answers

, . , .

using System.Threading;


public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        //Queues changes until after site exists.  For use in provisioning.
        SPWeb web = properties.Feature.Parent as SPWeb;
        ThreadPool.QueueUserWorkItem(ApplyYourChanges, web.Url);
    }

private void ApplyYourChanges(object state)
    {
        string webUrl = state as string;
        Uri uri = new Uri(webUrl);

        // additional conditions here -- perhaps check if a feature was activated
        while (!SPSite.Exists(uri))
        {
            Thread.Sleep(5000);
        }
        using (SPSite site = new SPSite(webUrl))
        {
            using (SPWeb web = site.OpenWeb())
            {
                //configure the quicklaunch menu
                configureQuickLaunch(web);
            }
        }
    }

public static void configureQuickLaunch(SPWeb spWeb)
    {            
        SPNavigationNodeCollection nodeCollection = spWeb.Navigation.QuickLaunch;
        SPNavigationNode heading = nodeCollection.Cast<SPNavigationNode>().FirstOrDefault(n => n.Title == headingNode);
        SPNavigationNode item = heading.Children.Cast<SPNavigationNode>().FirstOrDefault(n => n.Url == url);
            if(item == null)
            {
                item = new SPNavigationNode(nodeName, url);
                item = heading.Children.AddAsLast(item);
            }
    }
+1

, , - , QuickLaunch . , , , , , .

, QuickLaunch.Count == 0 , . , ;

SPNavigationNodeCollection nodes = web.Navigation.QuickLaunch;
SPNavigationNode node = new SPNavigationNode("Node Name", "Node URL", true);
nodes.AddAsFirst(node);
0

, QuickLaunch . , QuickLaunch , - . , .

If you want to programmatically configure QuickLaunch to have your own set of nodes programmatically, you must do this as follows:

SPNavigation nav = spWeb.Navigation;  
nav.UseShared = false;  
spWeb.Update();  

I think your score should be something other than zero at this point.

0
source

Source: https://habr.com/ru/post/1705227/


All Articles