SharePoint 2010: Configure Navigation Settings in a Sandbox Solution
Requirement
Sometime back 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.
General Approach
Following piece of code briefly explains how this can be done
//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 implement this in a 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 the question arises as to how can we configure these settings in the sandbox solution.
How to configure navigation settings for a sandbox scenario
There was no ready made solution available so 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.
I have documented all the below properties as per my findings using the SharePoint Manager.
__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 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 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. Hope this helps. :)
References
For further reading please refer to the following links for references
- http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.publishing.publishingweb%28v=office.14%29.aspx
- http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.publishing.publishingweb.includepagesinnavigation%28v=office.14%29.aspx
- http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.publishing.publishingweb.includesubsitesinnavigation%28v=office.14%29.aspx
- http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.addproperty%28v=office.14%29.aspx
- http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.getproperty%28v=office.14%29.aspx
Cheers,
Geetanjali