Monday, October 14, 2013

Customize Site Template List in SharePoint 2013 programmatically as a Feature

I recently had the opportunity to develop a solution which limits the site templates available when a site owner attempts to create new sub-sites to a specific list of sites. As with many specific development items, I found that there were not many examples of how to create this kind of solution, so I'm providing a simple solution which can be used as a good starting point in creating your own solution.

If your not familiar with SharePoint development, you will need to have a SharePoint 2013 environment and a test site to deploy the feature to. I would not select a Publishing site since the list of site templates on that site can be customized from the Site Settings page.

First, begin by creating an empty SharePoint 2013 Empty Project and selecting the site that will be used to by Visual Studio 2013 to deploy and test the feature.

Next, add a Feature to your solution.
Then, right click your feature and add an Event Receiver.

Add the following to the top of the Feature Receiver class file
using System.Collections.ObjectModel;

Next, modify your FeatureActivated code to look like this.


I will walk through this code briefly to help you understand what I have done.

First is the Null check. It's a good practice so it's there and foremost. Next, is the SPWeb which everyone should be familiar with and as you can see I'm assigning the web properties.Then I create a copy of the web templates as they currently exist. I need these in order to determine which ones will be applied to the new template collection. Next I create the new template collection that will be used to hold the new set of templates. Now for the fun part. After creating the new string array that will hold the names of the templates that I want available, I use a "foreach" loop to iterate through each array item. I use an inner "for" loop to match any array item with an item in the original template collection and if found assign that template to the new template collection. I then use the new template collection as the templates that are available on the site. Based on the entries in my array the list of available templates on a Project site looks like this.


You may have to enable the feature manually from within the Site Settings page. Also, it should be noted that I have specified a culture in my code, if you need to specify others or do not want to at all, you may need to do some homework and see how that will affect your outcomes.

The only thing left to do is ensure that you can deactivate these changes and make the original list of templates available again if necessary. You can do that by adding the following code to FeatureDeactivating.


We once again do a Null check because we should. The only signficant change is the AllowAllWebTemplates which will do all of the work of returning the list to default.

I hope this example helps many of you accomplish the same or similar task.

No comments:

Post a Comment