Thursday, August 16, 2007

Current Navigation, Global Navigation, siblings and PublishingWeb?

Yes yes.. that's what was in my head during my quest to change the following options in the Navigation page.

I managed to change the global navigation use the following code:

web.Navigation.UseShared = true;
web.Update;

So how do you change the current navigation? Well, remember that the 'Navigation' link only becomes available when you activate the Office Sharepoint Server Publishing Infrastructure feature? Searching the online SDK I found the following class : PublishingWeb. This class which uses SPWeb has the following property : NavigationShowSiblings. When you set this boolean to true, the current navigation section will change to the desired option (as selected in the screenshot)!

Together with my colleague Servé Hermans we (indepently) searched for the solution and it was quite scary when I found the solution he also, at the same time, found the same page and thus the solution ;)
 
You might wonder why on earth we want to do this programmatically.. well we are working with a migrated environment where we are replacing our custom navigation webpart with the quicklaunch showing all the subsites (securitytrimmed ofcourse!). In order to make this work properly we have to set all the individual subsites to use that 'current navigation' property. Basically the code looks like this :

SPSite site = new SPSite("http://<sitename>");
foreach (SPWeb web in site.AllWebs)
{
    try
    {
        if (PublishingWeb.IsPublishingWeb(web))
        {
            PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
            publishingWeb.NavigationShowSiblings = true;
            publishingWeb.Update();
        }
    }

    catch (Exception error)
    {
        Console.WriteLine(error.Message.ToString());
    }
                     
    web.Close();
    web.Dispose();
}
site.Close();
site.Dispose();

Technorati tags: ,

13 comments:

Mugsy said...

Great post. Could you do something similar to change the Show Subsites setting to True? We have over 2000 sites that are being migrated and the users really want the subsites exposed.

Robin Meuré said...

Nathalie,

sorry for the late response but I have the answer ;) There is a property on the publishingweb class called : IncludeSubSitesInNavigation. This is a boolean and explained right here :http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.publishing.publishingweb.includesubsitesinnavigation.aspx

So if you set this to "True" the option becomes checked.

Good luck!

Mugsy said...

Robin,

Thanks for the response. I just realized that we have been using a collaboration portal in our test environment. Does that mean this process won't work for me?

Robin Meuré said...

Depends if the Publishing Infrastructure feature is activated which I guess is.. you can check it on the sitecollection features link in the site settings.

Mugsy said...

You were right, I discovered that after I posted. I had to tweak the code a little, but it worked like a charm! Thanks for all your help.

Anonymous said...

Heya Robin,

Any idea how to set that option under Current Navigation to "Display only the navigation items below the current site"?

I've been digging through your post and the SDK, and no dice! Any help would be apprecited man.

Anonymous said...

Robin! Thanks for your post. I have been looking for a problem since one whole week about displaying the Pages in the navigation bar. I know i can do this by going to site actions->Modify Navigation-> Show Pages. I have 100's of pages and if i do that i screw the look of the site. I just need to display the pages at the navigation bar. Any help wou;d be greatly appreciated.

Ali.

Robin Meuré said...

Ali, which navigation bar are you referring to?

Anonymous said...

I have created a Site Navigation Control using CurrentNavSiteMapProvider. I am able to display all the site nodes according tot he navigation settings defined through modify navigation interface. The only point when it fails is when the user adds a new link to the current navigation and selects open in a new windows option. The link gets displayed but it opens it in the same window. I am unable to find the propert which could reaveal that a particular link should be opened in a new windo. Any kind of help will be deeply appreciated,
Thanks

Robin Meuré said...

Hi,

unfortunatly I've never gone that deep before in utilizing the sitemap providers so I also have no idea where that property is being set.

How are you enumerating through the sitemap?

Komakino said...

Just what I'm looking for!

Thank you!

Matthias Fonteyne said...

I just found that the Update method is not even required after switching the boolean value.
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.navigation.spnavigation.useshared.aspx

elSuizo said...

Thank you, this information looks temting to me. Alas, being a scripting novice, I have no idea how I can execute this kind of script; Put it into a file, I suppose, and give it a name like nav. and then, what extension? and how can I call it up?
Thanks for your help.
Michael