0

I am trying to develop a Sharepoint 2013 feature to add several navigation nodes in the top navigation bar of the web site on which the feature is activated. To achieve this, I have overriden only the FeatureActivated method of the event receiver. In case the feature is activated in a top level site, only two navigation nodes would be added to the bar (the main page node and the current site node). But if the feature is activated within a subsite, three navigation nodes should be added instead (main page, current site and subsite). My source code is as follows:

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {

        SPWeb web = (SPWeb)properties.Feature.Parent;


        SPNavigationNode node = new SPNavigationNode("Main", "http://homepage", true);
        web.Navigation.TopNavigationBar.AddAsLast(node);

        if (!web.IsRootWeb)
        {
            SPNavigationNode node1 = new SPNavigationNode("Site 1", web.ParentWeb.Url, true);
            web.Navigation.TopNavigationBar.AddAsLast(node1);
        }

        SPNavigationNode node2 = new SPNavigationNode("Site 2", web.Url, true);
        web.Navigation.TopNavigationBar.AddAsLast(node2);

    }

The problem I'm facing is that, when activated, the feature only adds the first ("Main") navigation node. I decided to remove the if statement and deploy the following code for testing purposes:

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {

        SPWeb web = (SPWeb)properties.Feature.Parent;


        SPNavigationNode node = new SPNavigationNode("Main", "http://homepage", true);
        web.Navigation.TopNavigationBar.AddAsLast(node);


        SPNavigationNode node2 = new SPNavigationNode("Site 2", web.Url, true);
        web.Navigation.TopNavigationBar.AddAsLast(node2);

    }

But again, only the first navigation node ("Main") was added. That made me think that the code was probably throwing an error when retrieving the url. Because of that, I decided to deploy the following code that basically puts null instead of an actual url and uses the web site url as the display name for both site and subsite nodes, just for the sake of testing:

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {

        SPWeb web = (SPWeb)properties.Feature.Parent;


        SPNavigationNode node = new SPNavigationNode("Main", "http://homepage", true);
        web.Navigation.TopNavigationBar.AddAsLast(node);

        if (!web.IsRootWeb)
        {
            SPNavigationNode node1 = new SPNavigationNode(web.ParentWeb.Url, null, true);
            web.Navigation.TopNavigationBar.AddAsLast(node1);
        }

        SPNavigationNode node2 = new SPNavigationNode(web.Url, null, true);
        web.Navigation.TopNavigationBar.AddAsLast(node2);

    }

This time, only the site and subsite nodes where added (and not the "Main" node). By the way, the urls were correctly printed. Can anyone help me understand what is causing this behavior?

0

After many hours I managed to solve the problem, though I still can't understand why it happened. Basically Sharepoint didn't render those nodes that pointed to the site/subsite on which they were being added (for instance, if I tried to activate the feature in, let's say, http://web-application/managed-path/site-collection/subsite/, the node that pointed to such subsite didn't appear, which is always the last node). So I decided to add first the node with a null url and update it afterwards. To my surprise, it did work:

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        using (SPWeb web = (SPWeb)properties.Feature.Parent)
        {
            SPNavigationNode nodeMain = new SPNavigationNode("Main", "http://homepage", true);
            web.Navigation.TopNavigationBar.AddAsLast(nodeMain);
            if (!web.IsRootWeb)
            {
                SPNavigationNode node1 = new SPNavigationNode(web.ParentWeb.Title, web.ParentWeb.Url, true);
                web.Navigation.TopNavigationBar.AddAsLast(node1);
            }
            SPNavigationNode node2 = new SPNavigationNode(web.Title, null, true);
            web.Navigation.TopNavigationBar.AddAsLast(node2);
            node2.Url = web.Url;
            node2.Update();
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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