• This email address is being protected from spambots. You need JavaScript enabled to view it.

    Recent items


Tuesday, 04 December 2012 11:22

Manipulating Global Navigation in SharePoint fails for publishing websites (code behind)

Written by
Rate this item
(1 Vote)

I've realized a custom web template based on Publishing Site Template (BaseTemplateName="CMSPUBLISHING" BaseTemplateID="39") used to create the site collection.

After the site collection creation, I had to add a Heading Link to Global Navigation. The test failed because the Navigation.TopNavigationBar object of root web was null.

As described into MSDN, the property is null when a site inherit the settings from parents, but I want to manipulate the root web that not have a parent web o.O.

After a first search, I found a solution (but this is not so clean).


public override void FeatureActivated(SPFeatureReceiverProperties properties)
    SPSite site = properties.Feature.Parent as SPSite;

    using (SPWeb web = site.OpenWeb("/your_web_url"))
        if (web.Navigation.TopNavigationBar == null)
            List<SPContentDatabase> contentdatabases = new List<SPContentDatabase>();

                SPNavigationNode node = new SPNavigationNode("", web.ServerRelativeUrl, false);

                web.AllowUnsafeUpdates = true;

                    SPNavigationNodeCollection navigationNodes = null;
                    navigationNodes = web.Navigation.GlobalNodes;

                    web.AllowUnsafeUpdates = false;

                SPContentDatabase database = site.ContentDatabase;

                using (SqlConnection con = new SqlConnection(database.DatabaseConnectionString))

                    using (SqlCommand command = con.CreateCommand())
                        command.CommandText = string.Format(@"UPDATE NavNodes
                        SET Url='', Eid={0}, ElementType=1, DocId=NULL
                        WHERE Eid={1}
                            and WebId='{2}'
                            and SiteId='{3}'",



This solution, add a row into NavNodes table of Content DB with an element that have Eid=1002. This node enable to manipulate the TopNavigationBar. This solution is not supported from Microsoft, so I cannot use it into production environment.

Fortunally, exists a good solution for this problem: adding the code below into the Onet.xml file, magically the TopNavigationBar property is not null :D

    <NavBar Name=”SharePoint Top Navbar” ID=”1002″ />

With this settings, the system provide to initialize the navigation so I can manipulate it codebehind.

Read 10009 times Last modified on Tuesday, 04 December 2012 11:44

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.