One of the common tasks that administrators will need to perform across various site collections upon initial deployment is to set which site templates (and page layouts) are available for creating subsites and pages. You can do this via the Site Collection Site Settings page (Site Settings -> Page Layout and Site Template Settings). If you need to script it like I did then you'll need to get a PublishingWeb object (via the static PublishingWeb.GetPublishingWeb() method which takes an SPWeb object as an argument) and then call the SetAvailableCrossLanguageWebTemplates() method and the SetAvailableWebTemplates() depending on whether you are setting a cross language (language neutral) template or a language specific template.

Enumerating the list of templates is just as easy - you simply call the GetAvailableWebTemplates() method or the GetAvailableCrossLanguageWebTemplates() method. One of the challenges I had was in determining exactly how Microsoft handles the setting of templates as it's not entirely as obvious as I indicate above due to the fact that a template name can be associated as a cross language template or a language specific template and it's not always clear how it's currently set. I did a lot of digging through Microsoft's code and I believe that I've got it all figured out.

The commands below allow you to enumerate the list of current available and installed templates (helpful for getting the real template names so that you can then call the add and remove commands) as well as add and remove available templates.

1. gl-enuminstalledsitetemplates

The code to enumerate the list of installed templates is extremely simple. We create a new SPSite object passing in the URL of the Site Collection and then we use an SPWeb object to get the list of installed languages which we will iterate through. Then, for each installed language we get the list of web templates and custom web templates by calling the SPSite objects GetWebTemplates() and GetCustomWebTemplates() methods, passing in the locale ID (LCID) we retrieved from the SPWeb.RegionalSettings.InstaleldLanguages.SPLanguage object:

The code to enumerate the list of available templates is almost identical to that needed to enumerate installed templates. The only real difference is that instead of getting the collection from the SPSite object you'll be getting it from a PublishingWeb object (not that there is no way to get a list of cross languange installed templates - this one of those things that threw me off - when you install the template it appears to be language specific but you can then make it available as a cross language template):

C:\>stsadm -help gl-enumavailablesitetemplates
stsadm -o gl-enumavailablesitetemplates
Returns the list of site templates available for the given site collection.
Parameters:
-url <site collection url>

Here’s an example of how to enumerate the list of available templates for a site collection:

stsadm –o gl-enumavailablesitetemplates –url "http://intranet/"

The results of executing this command can be seen below (your results may be different):

Note that the reason why some sites appear to be listed twice but only show up once in the web tool is because behind the scenes Microsoft is removing the duplicate (it knows that WIKI#0 is listed as a cross language template and it's locale is 1033 so don't show the locale specific entry if we're already showing as a language neutral template. I chose to not do this filtering so you could see everything that's going on - it wouldn't take much effort to change the code so that it list each item on one line and merely flags the fact that it's listed as cross language and what the specific locale is.

3. gl-addavailablesitetemplate

To add to the list of available templates we simply get a collection of currently available templates and then add to that collection. We then call SetAvailableCrossLanguageWebTemplates() or SetAvailableWebTemplates() passing in the modified collection. The method you call depends on whether the template is a language neutral template or not. The syntax of the resultant command can be seen below:

C:\>stsadm -help gl-addavailablesitetemplate
stsadm -o gl-addavailablesitetemplate
Adds a site template to the list of available templates for the given site collection.
Parameters:
-url <site collection url>
-template <template name>
[-lcid <locale id>]
[-resetallsubsites]

Here’s an example of how to add a template to the list of available templates for a site collection:

I tried to use gl-removeavailablesitetemplate on a web and a site collection without, then with -lcid parameter but both failed. The template (WIKI#0) is installed and available. Without -lcid parameter, the command returns “Template is not assigned.”. With -lcid 1033 parameter, it returns “The system cannot find the file specified. (Exception from HRESULT: 0×80070002)” and the site become buggy: on the _layouts/newsbweb.aspx page, all the templates are gone! Do you have any idea of the problem? Thanks a lot.

I follow your steps to list out the site templates available and then tried removing one site template and restart the IIS.

1. tsadm –o gl-enuminstalledsitetemplates -url2. It list out the site templates available.3. I wanted remove this site template “_GLOBAL_#0 = DP Workspace (1033)”3. i tried the remove command “stsadm –o gl-removeavailablesitetemplate –url “http:/intranet/” -template “_GLOBAL_#0″ -lcid 1033 -resetallsubsites”4. It shows operation completed5. I restarted the IIS6. Open the sharepoint home page7. Click Site Action – Create Site8. it throws an error as “File or arguments not valid for site template ‘|0′”9. Now I am lost and not able to put it back to the previous working condition. I check again via stsadm to show the list of available site templates and i can still see the template is still shown as available. But now I cant create a site from sharepoint.Is there anything I had done wrong here please. Is there any solution to solve the error. Thank you very much

You don’t want to remove the global template – it’s a hidden template and users can’t create sites off it (it’s used as a sort of marker as most every other template is effectively based on it – not really but that’s the net result).

Yes I did tried to add the site template back again “stsadm –o gl-addavailablesitetemplate –url “http://intranet/” -template “_GLOBAL_#0″ -lcid 1033 -resetallsubsites”It gives this error “File or arguments not valid for site template ‘|0′”

Hi,Yes I did try your stsadm command to put back the site template again but it shows an error.The error is “File or arguments not valid for site template ‘|0′”. Its the same error when i tried to create a site from the Site Action.

In the list of site templates displayed via the stsadm it shows a few site templates as this:“_GLOBAL_#1 = DP ProjectWorkspace (1033)_GLOBAL_#3 = DP ProjectWorkspaceV1 (1033)_GLOBAL_#0 = DP Workspace (1033)_GLOBAL_#2 = Issue_List (1033)“As you can see that i have these few workspace site templates and was trying to remote the “_GLOBAL_#0″ template which is not in use.All these workspace template is was created for Project Server (PWA) which are also used for Sharepoint as well. I am not sure if these have anything to cause this.

I’m not sure what’s going on then. I’d look at it myself but unfortunately I don’t have a 2007 machine available right now as I’ve just rebuilt my laptop and only have a 2010 machine running. There is probably a way to fix it but at the moment I just don’t know without being able to reproduce it.

Hi Gary,Thanks for the respone. Appreciate it very much. If you would like to take a look at my machine, we can have a remote session which i can sent to via mail. I have also sent Microsoft this error to see if they have any idea on this which yet to receive any reply. I am googling to see if there is any workaround which can allow the users to be able to create site for time being.

Because the boolean that specifies all templates allowed means that it doesn’t matter if it exists in the collection. It could have been written as this:
if (!web.AllWebTemplatesAllowed)
{
bool exists;
try
{
exists = (templateColl[templateName] != null);
}
catch (ArgumentException)
{
exists = false;
}
if (exists)
{
output = “Template is already installed.”;
return (int)ErrorCodes.GeneralError;
}
}