Question

i want to edit the SharePoint Quicklaunch. I put the following code into the Feature Activated EventReceiver.

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);
            }

            return null;

But the navigation is still null. Does anyone have any idea how I can solve this problem?

Was it helpful?

Solution

This error occurs if you access quicklaunch while site is being created.Below code causes the feature activated code to wait until the site collection has been created before executing.

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);
            }
    }

OTHER TIPS

you need to perform

spWeb.update()

for changes to be visible.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top