Today I came across with a scenario where I had the requirement to
configure the Navigation Settings (available in the Look And Feel section) so as
to have different quick launch or current navigation for different subsites. It
seemed to be a quite simple task where you can make use of the PublishingWeb
class and set the desired properties to configure global as well as current
navigation.
The above image
shows the Navigation option in the Look and Feel section in site settings.
The below screenshot shows the navigation settings page from where the user
can manually configure the navigation options.
Following
piece of code briefly explains how this can be done
PublishingWeb pubWeb =
PublishingWeb.GetPublishingWeb(currentweb);
//Include pages in the quick launch
pubWeb.Navigation.CurrentIncludePages = true;
//Include subsites in the global nav
pubWeb.Navigation.GlobalIncludeSubsites = true;
But here comes a twist when you work as a part of
sandbox solution. The
PublishingWeb class is a part of the Microsoft.SharePoint.Publishing assembly
which is not available in sandbox. As a result, you cannot use this class to
configure the navigation settings. Then how can we configure these settings in
the sandbox solution.
After much exploring through reflector and sharepoint manager, finally
sharepoint manager came to my rescue. I tried to do the settings through UI and
observed what was happening behind the scenes to the various properties of SPWeb
property bag. My findings were that there are few properties like below whose
value changes when we modify the various settings in the Navigation Settings
page.
__CurrentNavigationIncludeTypes :- The value of this
property changes when we try to select the various options in the Current
Navigation section. The various possible options are 0, 1, 2, 3
0 indicates that in structural navigation , nothing is selected
1 indicates that in structural navigation, Show
Subsites is checked and Show Pages is unchecked
2 indicates that in structural navigation, Show Subsites is unchecked
and Show Pages is checked
3 indicates that in structural navigation, Show Subsites is checked and
Show Pages is also checked
__GlobalNavigationIncludeTypes :- The value of this property
changes when we try to select the various options in the Global Navigation
section. The various possible options are 0,
0 indicates that in structural navigation , nothing is selected
1 indicates that in structural navigation, Show Subsites is checked and
Show Pages is unchecked
2 indicates that in structural navigation, Show Subsites is unchecked
and Show Pages is checked
3 indicates that in structural navigation, Show Subsites is checked and
Show Pages is also checked
__InheritCurrentNavigation :- This property if set to true
inherits the parent navigation in the child web.
__NavigationShowSiblings :- This property is a boolean value
that represents whether the web should display the siblings.
__IncludeSubSitesInNavigation :- This property is common to
both global navigation as well as current navigation. As the name suggests, it
will include subsites in the global as well as current navigation.
__IncludePagesInNavigation :- This property is common to
both global navigation as well as current navigation. As the name suggests, it
will include all the pages in the global as well as current navigation.
Now, in my scenario, the global as well as current navigation was supposed to
be different. Following is the piece of code
public
const string InheritCurrentNavigation =
"__InheritCurrentNavigation";
public const string CurrentNavigationIncludeTypes =
"__CurrentNavigationIncludeTypes";
public const string NavigationShowSiblings =
"__NavigationShowSiblings";
public const string GlobalNavigationIncludeTypes = "__GlobalNavigationIncludeTypes";
public override void
FeatureActivated(SPFeatureReceiverProperties
properties)
{
try
{
SPWeb web = properties.Feature.Parent as
SPWeb;
if (web.IsRootWeb !=
true)
{
//Set child web not to inherit from parent web
if (web.GetProperty(InheritCurrentNavigation) !=
null)
{
web.DeleteProperty(InheritCurrentNavigation);
web.AddProperty(InheritCurrentNavigation,
bool.FalseString);
}
else
{
web.AddProperty(InheritCurrentNavigation,
bool.FalseString);
}
if (web.GetProperty(NavigationShowSiblings) !=
null)
{
web.DeleteProperty(NavigationShowSiblings);
web.AddProperty(NavigationShowSiblings,
bool.FalseString);
}
else
{
web.AddProperty(NavigationShowSiblings,
bool.FalseString);
}
//Setting the Global navigation to show only the Subsites
if (web.GetProperty(GlobalNavigationIncludeTypes) != null)
{
web.DeleteProperty(GlobalNavigationIncludeTypes);
web.AddProperty(GlobalNavigationIncludeTypes, 1);
}
else
{
web.AddProperty(GlobalNavigationIncludeTypes, 1);
}
//Setting the current navigation to show only pages
if
(web.GetProperty(CurrentNavigationIncludeTypes) !=
null)
{
web.DeleteProperty(CurrentNavigationIncludeTypes);
web.AddProperty(CurrentNavigationIncludeTypes, 2);
}
else
{
web.AddProperty(CurrentNavigationIncludeTypes, 2);
}
web.Update();
}
}
catch (Exception ex)
{
}
}
With the help of above properties, you can easily configure navigation
settings in a sandbox solution as well. You can explore more properties with the
help of SharePoint Manager. Happy SharePointing.... :)