Tuesday, August 30, 2011

Hide Any Site template in Create Site section- SharePoint 2010

We may need to block creation of sites based on certain templates. For example my organisation does not like the employees to create blog site of their own in their personal mysite. 

There is a quick and dirty way to modify the webtemp.xml file to hide the template we dont want to show up. Its never a good approach to modify the default SharePoint config files. Also webtemp.xml file modification will affect farm level settings.

In SharePoint 2010 we can hide the templates of desired nature from the Create site page so no one can create site based on the particular template. This can by achieved by creating a new feature with a event handler code.

1. Create a new Empty SharePoint project.
2. Add a new feature element.Scope it to Web
3. Add a new event receiver to that feature element.
4. Paste  the below code(this code will hide the blog template at the site level)

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb web = properties.Feature.Parent as SPWeb;
SPWebTemplateCollection existingWebTemps = web.GetAvailableWebTemplates(1033); ;
Collection<SPWebTemplate> newWebTemps = new Collection<SPWebTemplate>();
for (int i = 0; i < existingWebTemps.Count; i++)
{
if (!existingWebTemps[i].Title.ToLower().Contains("blog"))
{
newWebTemps.Add(existingWebTemps[i]);
}
}
web.SetAvailableWebTemplates(newWebTemps, 1033);
web.Update();
}

5.Deploy the solution and activate the feature. You will no longer see the blog template visible to the user in the Create site page.

6. To revert back and to show all the templates use the below code at Feature deactivating event.

 web.AllowAllWebTemplates() ;

3 comments:

  1. Hi Suresh,

    thank you for your post, is very usefull.
    Now I have another question...

    Do you think that is possible to implement the same limitation at Web Application level?
    My requirement is to limit the Site Collection Templates available on one Web Application. I've found only a solution that impact all the web applications of the Farm (editing the webtemp.xml files).

    Thank you in advance,

    Diego.


    ReplyDelete
  2. Diego,

    You cannot hide the site templates just for one web application. Reason is Site collections has to be created from Central admin and Central admin is not restricted to any web application rather you choose the web application for the site collection created.

    I am not sure whats your requirement is. But Site collections has to be created only by the site administrator. In that case restricting the site templates doesn't impact. Or if you are providing the feature of creating Site Collection by the user itself, the best approach is to create a custom application page and provide the create site collection functionality and restrict the available templates there. Because its never recommended to allow users to use the Central Admin.

    Thanks.
    Suresh

    ReplyDelete
  3. If you want to enable such a web feature within all site collections (--> all rootwebs and subwebs/subsites) of a web application, you could use a web application scoped "feature stapler feature" that staples this web feature to all templates in this web application.

    After that, users will not see these templates, when they want to create subsites.

    ReplyDelete