in

didierdanse.net

Site personnel de Didier Danse
Didier Danse's Personnal Website
Microsoft Most Valuable Professional SharePoint

didierdanse.net - Development News

SharePoint: How to limit available site templates for site creation

Once again, a colleague ask me if there is some possibilities to remove some site templates from the site creation page as she don’t want to allow power users to create sites based on custom site templates.

The easiest way to do this is to click on Page Layouts and Site Templates on the Site Settings page. This link points at //_Layouts/AreaTemplateSettings.aspx">//_Layouts/AreaTemplateSettings.aspx">http://<yoursharepointurl>/<sitename>/_Layouts/AreaTemplateSettings.aspx page. This link is only available at the root site of the site collection but you can navigate to the page using the previous link.

On this page, you will be able able to select site templates from available site templates list: 

From a developer or an administrator perspective, there is also the possibility to use some code to manage available site templates.

Here is a example of this code:

public void SetAvailableTemplates(List<string> templateNames)
{
     SetAvailableTemplates(templateNames, 0);
}

public void SetAvailableTemplates(List<string> templateNames, uint lcid)
{
    using (SPSite site = new SPSite("http://<yoursharepointurl>/<sitename>"))
    {
        using (SPWeb web = site.OpenWeb())
        {
            if (lcid == 0) lcid = web.Language;
            Collection<SPWebTemplate> availablesTemplates = new Collection<SPWebTemplate>;
            foreach (SPWebTemplate template in site.GetWebTemplates(lcid))
                if (templateNames.Keys.Contains(template.Name))
                    availablesTemplates.Add(template);
        
            if (availablesTemplates.Count > 0)
            {
               // Code is different if site is a publishing site or not as some properties and methods are not exposed directly
                if (PublishingWeb.IsPublishingWeb(web))
                {
                    PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
                    if (lcid == 0) publishingWeb.SetAvailableCrossLanguageWebTemplates(availablesTemplates, false);
                    else publishingWeb.SetAvailableWebTemplates(availablesTemplates, lcid, false);
                }
                else
                {
                    if (lcid == 0) web.SetAvailableCrossLanguageWebTemplates(availablesTemplates);
                    else web.SetAvailableWebTemplates(availablesTemplates, lcid);
                    web.AllProperties["__InheritWebTemplates"] = false.ToString();
                    web.Update();
                }
            }
        }
    }
}

Now you can package this as a STSADM extension, a feature or anything else that can be imagined to manipulate your SharePoint.



Comments

No Comments

Leave a Comment

(required)  
(optional)
(required)  
Add
L'auteur du site ne peut être tenu responsable des dommages que les informations fournies pourraient entraîner. Tout est cependant mis en oeuvre pour éviter tout désagrément.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems