HOWTO Use Features in SharePoint 2010 Site Template Definitions

Site template definitions provide a method of creating web site templates that can be used to create new sites in a SharePoint 2010 site collection. SharePoint 2010 provides a large number of site templates, and these can be copied and modifed to fit your purpose.

There are already excellent resources on the web to learn about creating site template definitions and advanced discussions on feature stapling, so I’m focusing this blog on how to use features and set properties, using the NavigationProperties feature as an example.

If you run the following powershell code:

Get-SPFeature

you’ll get a large list of the installed SharePoint features in your environment. These features can be used within your site template definitions to automatically enable certain features within a site during its creation. Refining this further using:

$feature = Get-SPFeature -identity NavigationProperties

will set the $feature variable and allow you to print out it’s full list information using:

$feature | fl

In particular, note the following values:

Id                          : 541f5f57-c847-4e16-b59a-b31e90e6f9ea

Scope                       : Web

ReceiverClass               : Microsoft.SharePoint.Publishing.NavigationFeatureHandler

The Id uniquely identifies the feature, the scope tells us what sort of site template definition we can use it in (in the example here I used a Team Site, so “Web” is the correct scope to use – a “Site” scoped feature would not work, and would generate an error when trying to create the site if it was placed in the SiteFeatures element instead of WebFeatures). The receiver class we will use soon. So we add our feature into the onet.xml file as follows:

<WebFeatures>
<!– Navigation properties for global and current navigation –>
<Feature ID=”541F5F57-C847-4e16-B59A-B31E90E6F9EA”>
<Properties xmlns=”http://schemas.microsoft.com/sharepoint/“>
</Properties>
</Feature>
</WebFeatures>

We also want to set some properties for our feature, and for this I am going to use the Assembly viewer ILSpy. I loaded up the assembly Microsoft.SharePoint.Publishing and found the reference for Microsoft.SharePoint.Publishing.NavigationFeatureHandler, which was specified in the “ReceiverClass” property above. This gives me a complete list of the properties available and how they are used in the “ApplyNavigationProperties” method as follows:

switch (name = sPFeatureProperty.Name)
{
case “InheritGlobalNavigation”:
{
publishingWeb.Navigation.InheritGlobal = bool.Parse(sPFeatureProperty.Value);
break;
}
case “InheritCurrentNavigation”:
{
publishingWeb.Navigation.InheritCurrent = bool.Parse(sPFeatureProperty.Value);
break;
}
case “ShowSiblings”:
{
publishingWeb.Navigation.ShowSiblings = bool.Parse(sPFeatureProperty.Value);
break;
}
case “IncludeSubSites”:
{
bool flag = bool.Parse(sPFeatureProperty.Value);
publishingWeb.Navigation.GlobalIncludeSubSites = flag;
publishingWeb.Navigation.CurrentIncludeSubSites = flag;
break;
}
case “IncludePages”:
{
bool flag2 = bool.Parse(sPFeatureProperty.Value);
publishingWeb.Navigation.GlobalIncludePages = flag2;
publishingWeb.Navigation.CurrentIncludePages = flag2;
break;
}
case “GlobalIncludeSubSites”:
{
publishingWeb.Navigation.GlobalIncludeSubSites = bool.Parse(sPFeatureProperty.Value);
break;
}
case “GlobalIncludePages”:
{
publishingWeb.Navigation.GlobalIncludePages = bool.Parse(sPFeatureProperty.Value);
break;
}
case “CurrentIncludeSubSites”:
{
publishingWeb.Navigation.CurrentIncludeSubSites = bool.Parse(sPFeatureProperty.Value);
break;
}
case “CurrentIncludePages”:
{
publishingWeb.Navigation.CurrentIncludePages = bool.Parse(sPFeatureProperty.Value);
break;
}
case “GlobalDynamicChildLimit”:
{
publishingWeb.Navigation.GlobalDynamicChildLimit = int.Parse(sPFeatureProperty.Value);
break;
}
case “CurrentDynamicChildLimit”:
{
publishingWeb.Navigation.CurrentDynamicChildLimit = int.Parse(sPFeatureProperty.Value);
break;
}
case “OrderingMethod”:
{
publishingWeb.Navigation.OrderingMethod = (OrderingMethod)Enum.Parse(typeof(OrderingMethod), sPFeatureProperty.Value);
break;
}
case “AutomaticSortingMathod”:
case “AutomaticSortingMethod”:
{
publishingWeb.Navigation.AutomaticSortingMethod = (AutomaticSortingMethod)Enum.Parse(typeof(AutomaticSortingMethod), sPFeatureProperty.Value);
break;
}
case “SortAscending”:
{
publishingWeb.Navigation.SortAscending = bool.Parse(sPFeatureProperty.Value);
break;
}
case “IncludeInGlobalNavigation”:
{
publishingWeb.IncludeInGlobalNavigation = bool.Parse(sPFeatureProperty.Value);
break;
}
case “IncludeInCurrentNavigation”:
{
publishingWeb.IncludeInCurrentNavigation = bool.Parse(sPFeatureProperty.Value);
break;
}

Interestingly, setting some properties (such as IncludeSubSites and IncludePages) actually sets two different properties in the navigation. However, I can pick-and-choose what properties I want to set:

<!– Navigation properties for global and current navigation –>
<Feature ID=”541F5F57-C847-4e16-B59A-B31E90E6F9EA”>
<Properties xmlns=”http://schemas.microsoft.com/sharepoint/“>
<Property Key=”InheritGlobalNavigation” value=”false” />
<Property Key=”InheritCurrentNavigation” value=”false” />
<Property Key=”GlobalIncludeSubSites” value=”true” />
<Property Key=”GlobalIncludePages” value=”true” />
<Property Key=”CurrentIncludeSubSites” value=”false” />
<Property Key=”CurrentIncludePages” value=”false” />
<Property Key=”IncludeInGlobalNavigation” value=”true” />
<Property Key=”IncludeInCurrentNavigation” value=”true” />
</Properties>
</Feature>

Now when I create my new site based on the site template definition, the navigation settings are already set for me. You can check them by using Site Settings, Navigation, and reviewing what properties have been set.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: