Blogger

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

    Recent items

Login

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).

{codecitation}

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

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                SPNavigationNode node = new SPNavigationNode("", web.ServerRelativeUrl, false);

                web.AllowUnsafeUpdates = true;

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

                    navigationNodes.AddAsFirst(node);
                }
                finally
                {
                    web.AllowUnsafeUpdates = false;
                }

                SPContentDatabase database = site.ContentDatabase;

                using (SqlConnection con = new SqlConnection(database.DatabaseConnectionString))
                {
                    con.Open();

                    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}'",
                            1002,
                            node.Id,
                            web.ID.ToString(),
                            site.ID.ToString()
                        );

                        command.ExecuteNonQuery();
                    }
                }
            });
        }
    }
}

{/codecitation}

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

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

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

Read 9390 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.