Saturday, 1 June 2013

Sharepoint 2013 - Create a site from a custom site-template using client object model

Here, is the code to create a site from a custom site-template using CSOM.

function CreateSite() {

        //load site
   var currentcontext = new SP.ClientContext.get_current();
   var currentweb = currentcontext.get_web();
   //site object
   var webCreateInfo = new SP.WebCreationInformation();
   //set values
   webCreateInfo.set_description("This site is created from CSOM");
   webCreateInfo.set_title("My Title");
   //add sub site
   this.NewWebsite = this.currentweb.get_webs().add(webCreateInfo);
   //Load and execute query
   currentcontext.load(this.NewWebsite, 'ServerRelativeUrl', 'Created');
   currentcontext.executeQueryAsync(Function.createDelegate(this, this.Success), Function.createDelegate(this, this.oncListQueryFailed));    

function Success() {

function oncListQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());

Template ID is the most important thing here.It defines what is the type of site we are creating like would it be a Team Site,Blank Site, Wiki, Blog etc.
We can find all default template ID from here

But , since here we are using our own custom site template . So we can get our template id from the code given in this blog.

Get web-template

Your custom site template id would  be in this format.