when we create a new site means always refer to an existing template, and often to a custom template. Here’s how to retrieve the list of templates to create a new web site
function BTGnGetWebTemplates(weburl){
var context = new SP.ClientContext(weburl);
var web = context.get_web();
templateCollection = web.getAvailableWebTemplates(1033, false);
context.load(templateCollection);
context.executeQueryAsync(Function.createDelegate(this, this.onBTGnGetWebTemplatesSucccess), Function.createDelegate(this, this.onBTGnGetWebTemplatesFailed));
}
function onBTGnGetWebTemplatesSucccess() {
var Templates = “”;
var siteTemplatesEnum = templateCollection.getEnumerator();
while(siteTemplatesEnum.moveNext())
{
var siteTemplate = siteTemplatesEnum.get_current();
Templates += siteTemplate.get_name() + ‘\n’;
}
console.log(“Site Templates – ” + ‘,’ + Templates);
}