Wednesday, October 30, 2013

Declaratively configure navigation settings in Sharepoint publishing sites

If you have custom web template based on OTB publishing site (CMSPUBLISHING) you can configure navigation settings declaratively by adding NavigationProperties feature (id = 541F5F57-C847-4e16-B59A-B31E90E6F9EA) to your onet.xml and defining properties, e.g.:

   1: <Feature ID="541F5F57-C847-4e16-B59A-B31E90E6F9EA">
   2:   <Properties xmlns="http://schemas.microsoft.com/sharepoint/">
   3:     <Property Key="InheritGlobalNavigation" Value="true"/>
   4:     <Property Key="IncludeSubSites" Value="true"/>
   5:     <Property Key="IncludePages" Value="false"/>
   6:     <Property Key="IncludeInGlobalNavigation" Value="false"/>
   7:     <Property Key="IncludeInCurrentNavigation" Value="false"/>
   8:   </Properties>
   9: </Feature>

What other properties are supported? In order to answer on this question let’s check NavigationFeatureHandler feature receiver (from Microsoft.SharePoint.Publishing assembly). In its FeatureActivated method it uses private method ApplyNavigationProperties which does actual job:

   1:  
   2: private static void ApplyNavigationProperties(PublishingWeb publishingWeb,
   3: SPFeaturePropertyCollection properties)
   4: {
   5:     if (!publishingWeb.Web.AllProperties.ContainsKey("NavigationPropertiesSet"))
   6:     {
   7:         CmsSecurityUtilities.RunWithWebCulture(publishingWeb.Web, delegate {
   8:             CommonUtilities.ConfirmNotCentralAdminWebApp(publishingWeb.Web);
   9:             for (int j = 0; j < properties.Count; j++)
  10:             {
  11:                 SPFeatureProperty property = properties[j] as SPFeatureProperty;
  12:                 switch (property.Name)
  13:                 {
  14:                     case "InheritGlobalNavigation":
  15:                         publishingWeb.Navigation.InheritGlobal =
  16:                             bool.Parse(property.Value);
  17:                         break;
  18:  
  19:                     case "InheritCurrentNavigation":
  20:                         publishingWeb.Navigation.InheritCurrent =
  21:                             bool.Parse(property.Value);
  22:                         break;
  23:  
  24:                     case "ShowSiblings":
  25:                         publishingWeb.Navigation.ShowSiblings =
  26:                             bool.Parse(property.Value);
  27:                         break;
  28:  
  29:                     case "IncludeSubSites":
  30:                     {
  31:                         bool flag = bool.Parse(property.Value);
  32:                         publishingWeb.Navigation.GlobalIncludeSubSites = flag;
  33:                         publishingWeb.Navigation.CurrentIncludeSubSites = flag;
  34:                         break;
  35:                     }
  36:                     case "IncludePages":
  37:                     {
  38:                         bool flag2 = bool.Parse(property.Value);
  39:                         publishingWeb.Navigation.GlobalIncludePages = flag2;
  40:                         publishingWeb.Navigation.CurrentIncludePages = flag2;
  41:                         break;
  42:                     }
  43:                     case "GlobalIncludeSubSites":
  44:                         publishingWeb.Navigation.GlobalIncludeSubSites =
  45:                             bool.Parse(property.Value);
  46:                         break;
  47:  
  48:                     case "GlobalIncludePages":
  49:                         publishingWeb.Navigation.GlobalIncludePages =
  50:                             bool.Parse(property.Value);
  51:                         break;
  52:  
  53:                     case "CurrentIncludeSubSites":
  54:                         publishingWeb.Navigation.CurrentIncludeSubSites =
  55:                             bool.Parse(property.Value);
  56:                         break;
  57:  
  58:                     case "CurrentIncludePages":
  59:                         publishingWeb.Navigation.CurrentIncludePages =
  60:                             bool.Parse(property.Value);
  61:                         break;
  62:  
  63:                     case "GlobalDynamicChildLimit":
  64:                         publishingWeb.Navigation.GlobalDynamicChildLimit =
  65:                             int.Parse(property.Value);
  66:                         break;
  67:  
  68:                     case "CurrentDynamicChildLimit":
  69:                         publishingWeb.Navigation.CurrentDynamicChildLimit =
  70:                             int.Parse(property.Value);
  71:                         break;
  72:  
  73:                     case "OrderingMethod":
  74:                         publishingWeb.Navigation.OrderingMethod =
  75: (OrderingMethod) Enum.Parse(typeof(OrderingMethod), property.Value);
  76:                         break;
  77:  
  78:                     case "AutomaticSortingMathod":
  79:                     case "AutomaticSortingMethod":
  80:                         publishingWeb.Navigation.AutomaticSortingMethod =
  81: (AutomaticSortingMethod) Enum.Parse(typeof(AutomaticSortingMethod), property.Value);
  82:                         break;
  83:  
  84:                     case "SortAscending":
  85:                         publishingWeb.Navigation.SortAscending =
  86:                             bool.Parse(property.Value);
  87:                         break;
  88:  
  89:                     case "IncludeInGlobalNavigation":
  90:                         publishingWeb.IncludeInGlobalNavigation =
  91:                             bool.Parse(property.Value);
  92:                         break;
  93:  
  94:                     case "IncludeInCurrentNavigation":
  95:                         publishingWeb.IncludeInCurrentNavigation =
  96:                             bool.Parse(property.Value);
  97:                         break;
  98:                 }
  99:             }
 100:             publishingWeb.SetBooleanProperty("NavigationPropertiesSet", true);
 101:             publishingWeb.Update();
 102:         });
 103:     }
 104: }

So here we can see all supported properties:

Supported navigation properties
InheritGlobalNavigation
InheritCurrentNavigation
ShowSiblings
IncludeSubSites
IncludePages
GlobalIncludeSubSites
GlobalIncludePages
CurrentIncludeSubSites
CurrentIncludePages
GlobalDynamicChildLimit
CurrentDynamicChildLimit
OrderingMethod
AutomaticSortingMathod/AutomaticSortingMethod
SortAscending
IncludeInGlobalNavigation
IncludeInCurrentNavigation

You can use these properties in your onet.xml files.

No comments:

Post a Comment