3

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?

1
  • what scope does the feature have? and where is this spweb created?
    – Gwny
    Commented Aug 11, 2015 at 11:32

2 Answers 2

2

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

you need to perform

spWeb.update()

for changes to be visible.

3
  • My error appears earlier. I got an Object Reference Exception in the first line of code. In my case spWeb.Navigation.QuickLaunch returns null.
    – Jan
    Commented Aug 11, 2015 at 11:26
  • can you write whole code, including spsite and spweb
    – Taran Goel
    Commented Aug 11, 2015 at 11:45
  • I seem to remember reading somewhere that the QuickLaunch collection only stored customisations to the default ordering. Looking around, I can't find that documentation to show you, but it would explain the behavior you see if true. So your QuickLaunch.Count == 0 is just confirming that default ordering of items is in place. You can still add nodes, if that's at all helpful; SPNavigationNodeCollection nodes = web.Navigation.QuickLaunch; SPNavigationNode node = new SPNavigationNode("Node Name", "Node URL", true); nodes.AddAsFirst(node);
    – Taran Goel
    Commented Aug 11, 2015 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.