Skip to main content

Weblog Ton Stegeman [MVP]

Go Search
Home
  

ODC 2008
If you have a question or suggestion, please contact me through Windows Live Messenger.
My status: .

If I am not online, please send me an e-mail.
Weblog Ton Stegeman [MVP] > Posts > Limiting the number of available site templates in code
Limiting the number of available site templates in code

In SharePoint you can limit the site templates the site templates that are available to users when they create a subsite. To do this you navigate to the page "Page layouts and site templates" from the site settings. For WSS there is no link to the UI to do this. You can limit the templates by manually entering the URL to the AreaTemplateSettings.aspx page.
In code there are 2 ways to do it. You can make a template available for all languages that are installed on the SharePoint server, or you can make it available just for a specific lanugage. The examples below show you how to do this, both for a publishing site and for a teamsite.

Example 1

This code snippet forces the users of our site to just use the "Team Site" template. The team site will be available in any language installed on the server. The code first finds the template using the GetWebTemplates of our SPSite object. To make it available for all languages, we use the method SetAvailableCrossLanguageWebTemplates.

This example works for both publishing sites and team sites. Although team sites do not have the site settings link to this, you can limit the number of available site templates by using the same method as on a publishingweb. You will have to set one of the properties in the property bag of the SPWeb to make it work.

using (SPSite site = new SPSite("http://moss/news"))
{
    using (SPWeb web = site.OpenWeb())
    {
        // Find the Team Site template.
        SPWebTemplate templateToAdd = null;
        foreach (SPWebTemplate template in site.GetWebTemplates(web.Language))
        {
            if (string.Compare("STS#0", template.Name, true) == 0)
            {
                templateToAdd = template;
                break;
            }
        }
        if (templateToAdd != null)
        {
            // Create a new collection of available templates
            Collection<SPWebTemplate> templates = new Collection<SPWebTemplate>();
            templates.Add(templateToAdd);

            // Update the web and save the changes
            if (PublishingWeb.IsPublishingWeb(web))
            {
                PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
                publishingWeb.SetAvailableCrossLanguageWebTemplates(templates, false);
            }
            else
            {
                web.SetAvailableCrossLanguageWebTemplates(templates);
                web.AllProperties["__InheritWebTemplates"] = false.ToString();
                web.Update();
            }
        }
    }
}

After running this, the "Page layouts and site templates" page looks like this:

image

Example 2

The second example does the same thing, but users are now forced to use the Dutch Team Site template. Instead of using SetAvailableCrossLanguageWebTemplates, we now use SetAvailableWebTemplates:

using (SPSite site = new SPSite("http://moss/news"))
{
    using (SPWeb web = site.OpenWeb())
    {
        // Find the Team Site template.
        SPWebTemplate templateToAdd = null;
        foreach (SPWebTemplate template in site.GetWebTemplates(1043))
        {
            if (string.Compare("STS#0", template.Name, true) == 0)
            {
                templateToAdd = template;
                break;
            }
        }
        if (templateToAdd != null)
        {
            // Create a new collection of available templates
            Collection<SPWebTemplate> templates = new Collection<SPWebTemplate>();
            templates.Add(templateToAdd);

            // Update the web and save the changes
            if (PublishingWeb.IsPublishingWeb(web))
            {
                PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
                publishingWeb.SetAvailableWebTemplates(templates, 1043, false);
            }
            else
            {
                web.SetAvailableWebTemplates(templates, 1043);
                web.AllProperties["__InheritWebTemplates"] = false.ToString();
                web.Update();
            }
        }
    }
}

After running this sample, the "Page layouts and site templates" page looks like this:

image

Comments

There are no comments yet for this post.
Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Body *


Your city *


Type the name of the city you live in (making it easier to handle spam...)

CurrentDate *

Select the current date (see if this gives me fewer spam...)
Attachments

 Links

  SharePoint Object on CodePlex
  Screencast introducing SharePoint Objects
  Content by Type and Filter Web Parts on CodePlex
  Archive
  Archive (Calendar)

 My Latest Blog Posts

Scripting SharePoint 2007 setup: choices and conceptsUse SHIFT+ENTER to open the menu (new window).
Adventures in Visual Studio 2010: Migrate the Content By Type web part to SharePoint 2010Use SHIFT+ENTER to open the menu (new window).
Register SharePoint themes by using a featureUse SHIFT+ENTER to open the menu (new window).
SharePoint 2010 development on Windows 2008 Server R2 – Getting StartedUse SHIFT+ENTER to open the menu (new window).
New release SharePoint Objects: features and groupsUse SHIFT+ENTER to open the menu (new window).
Constructing the url to the SharePoint Edit Permissions pageUse SHIFT+ENTER to open the menu (new window).
Screencast: introduction to SharePoint ObjectsUse SHIFT+ENTER to open the menu (new window).
SharePoint 2007 and Reporting ServicesUse SHIFT+ENTER to open the menu (new window).
SharePoint Objects – Insight in usage of your SharePoint artifactsUse SHIFT+ENTER to open the menu (new window).
SharePoint 2007 Custom list schema and the Content Query Web PartUse SHIFT+ENTER to open the menu (new window).
SharePoint 2010 Silverlight Client Object Model – ExecuteQuery vs ExecuteQueryAsyncUse SHIFT+ENTER to open the menu (new window).
SharePoint 2010, the Client Object Models and Bing MapsUse SHIFT+ENTER to open the menu (new window).
Having fun with SharePoint 2010, Silverlight 3 and Bing MapsUse SHIFT+ENTER to open the menu (new window).
Connecting TFS 2010 projects to SharePoint sitesUse SHIFT+ENTER to open the menu (new window).
Adding a database to the SharePoint database Server using SPDatabaseUse SHIFT+ENTER to open the menu (new window).