Saturday, 1 June 2013

Sharepoint 2013 - Get all Site templates using Client object model

Code to get all site templates both inbuilt and custom site template ID using CSOM.


var templateCollection ;
function GetWebTemplates()
{

var context = new SP.ClientContext.get_current();
var web = context.get_web();

templateCollection = web.getAvailableWebTemplates(1033, false);

context.load(templateCollection);
context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));
}

function success() {

var Templates = "";
var siteTemplatesEnum = templateCollection.getEnumerator();

while(siteTemplatesEnum.moveNext())
{
var siteTemplate = siteTemplatesEnum.get_current();
Templates +=  siteTemplate.get_name() +   ',';
}

alert("Site Templates - " + ',' + Templates);
}

function failed(sender, args) {
alert("Failed");
}


While you can get all the inbuilt site template ID from below link

And custom site template would have a format like this.
{GUID}#TemplateName