Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have following code

var subWeb = newWeb.Webs.Add(subsite.Url, subsite.Title, subsite.Title, 1033, PublishTemplate, false, false);
subWeb.AllowUnsafeUpdates = true;
subWeb.Update();
PublishingWeb publiSubWeb = PublishingWeb.GetPublishingWeb(subWeb);
publiSubWeb.Navigation.InheritGlobal = true;
publiSubWeb.Navigation.GlobalIncludeSubSites = true;
publiSubWeb.Navigation.GlobalIncludePages = true; // <-- Does not work 
publiSubWeb.Navigation.CurrentIncludePages = true; // <-- Does  not work
publiSubWeb.Navigation.CurrentIncludeSubSites = true;
publiSubWeb.Navigation.SortAscending = false;
publiSubWeb.Update();
subWeb.Update();

But it seems that the navigation properties "GlobalIncludePages" en "CurrentIncludePages" are not working. When I browse to the Navigation site (Site Settings) the "Show Pages" in the Current & Global Navigation is still unchecked... Also the property "ShowSiblings" (in other code by me) does not work.

I already put AllowUnsafeUpdates and some extra web updates in my code; but also this does not help.

UPDATE:
I am using a sandboxed solution. When I execute the same code in a console application it works. Strange. Is this really a limit of the sandbox???

Any ideas?

Thx.

share|improve this question
    
Seems that none of the properties can be set. So it does not work at all... –  Davy Jun 14 '12 at 15:15
    
I am using a sandboxed solution. When I execute the same code in a console application it works. Strange. Is this really a limit of the sandbox??? –  Davy Jun 14 '12 at 15:16
    
Hi @Davy. I'm also working on a sandbox solution and need to initialise the navigation settings on my publishign sites. Did you ever find a workaround? –  Nick Larter Aug 9 '12 at 6:25
    
Nick, i am sorry but i never found a solution to do it in sandbox. –  Davy Aug 9 '12 at 13:20
add comment

3 Answers

up vote 2 down vote accepted

I managed to solve this one today.

@Vardhaman is correct: the Microsoft.SharePoint.Publishing.dll assembly simply isn't sandbox compatible -- so none of the methods or properties in this assembly will work (even though Visual Studio doesn't throw any warnings or errors at compile-time!). If you reflect this assembly though, you can see that the navigation settings are really just stored as simple property bag values on the SPWeb object. This is the list of property bag keys that control the navigation settings internally, they're all self-explanatory:

    // Common (applies to both global and current navigation):
    public const string IncludeSubsitesKey = "__IncludeSubSitesInNavigation";
    public const string IncludePagesKey = "__IncludePagesInNavigation";

    // Global navigation:
    public const string GlobalNavigationDynamicChildLimitKey = "__GlobalDynamicChildLimit";

    // Current navigation:
    public const string CurrentNavigationInheritKey = "__InheritCurrentNavigation";
    public const string CurrentNavigationShowSiblingsKey = "__NavigationShowSiblings";
    public const string CurrentNavigationOrderingMethodKey = "__NavigationOrderingMethod";
    public const string CurrentNavigationAutomaticSortingMethodKey = "__NavigationAutomaticSortingMethod";
    public const string CurrentNavigationSortAscendingKey = "__NavigationSortAscending";
    public const string CurrentNavigationDynamicChildLimitKey = "__CurrentDynamicChildLimit";

The great news is that you can simply add these keys to the property bag yourself in order to achieve the same result as setting the properties in the Microsoft.SharePoint.Publishing.Navigation class. See below for an example:

    if (web.GetProperty(CurrentNavigationInheritKey) != null)
        web.DeleteProperty(CurrentNavigationInheritKey);

    web.AddProperty(CurrentNavigationInheritKey, bool.FalseString);

Note: Another undocumented bizzarre quirk of the sandbox is that the property bag can only be manipulated using the following methods:

    SPWeb.AddProperty(key, value)
    SPWeb.DeleteProperty(key)
    SPWeb.GetProperty(key)
    SPWeb.SetProperty(key, value)

The following alternate methods of accessing the property bag will only work in a non-sandboxed solution. Interestly, exceptions aren't thrown at runtime if you use these methods, however null values will always be returned and new values will simply never be persisted... So don't use them!

    SPWeb.AllProperties[key, value]
    SPWeb.Properties[key, value]
share|improve this answer
    
That's Awesome! In Sandbox, we have to think of novel ways like this to achieve simple tasks :) –  Vardhaman Deshpande Aug 10 '12 at 18:12
    
One more thing to add... If you create your subsite using a custom WebTemplate instead, then you can configure the navigation by simply passing in the appropriate feature properties. See below: <!-- Per-Web Portal Navigation Properties --> <Feature ID="541F5F57-C847-4e16-B59A-B31E90E6F9EA"> <Properties xmlns="http://schemas.microsoft.com/sharepoint/"> <Property Key="ShowSiblings" Value="false" /> <Property Key="IncludeSubSites" Value="true" /> <Property Key="IncludePages" Value="false" /> </Properties> </Feature> I can confirm that this works in a sandbox solution. –  Nick Larter Aug 14 '12 at 7:30
add comment

Though this thread is already marked as answer, still would like to add more to it. As mentioned above,"_IncludeSubsitesInNavigation" and "_IncludePagesInNavigation" properties can be added to the SPWeb property bag and made to work in sandbox scenario. Now these two properties are common for global as well as current navigation. For a scenario where we would like different values for these properties for global and current navigation, this will not work. However there are properties like "_GlobalNavigationIncludeTypes" and "_CurrentNavigationIncludeTypes" that comes to the rescue in such a situation. I have written a detailed blog on both these properties. Anybody facing a similar issue can have a look for the reference

http://community.zevenseas.com/Blogs/Geetanjali/Lists/Posts/Post.aspx?ID=16

Cheers, Geetanjali

share|improve this answer
add comment

The PublishingWeb class is a part of the Microsoft.SharePoint.Publishing library. This library is not allowed in the Sandbox. Instead of Microsoft.SharePoint.Publishing.PublishingWeb.Navigation, you can try using the Microsoft.SharePoint.Navigation class which is allowed in the sandbox.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.