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_language(1033);
   webCreateInfo.set_title("My Title");
   webCreateInfo.set_url("/myURL");
   webCreateInfo.set_useSamePermissionsAsParentSite(true);
   webCreateInfo.set_webTemplate("MyTemplateID");
   //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() {
alert(this.NewWebsite.get_serverRelativeUrl());
}

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 
https://www.nothingbutsharepoint.com/sites/devwiki/sp2010dev/pages/site%20templates%20in%20sharepoint%202010.aspx

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.
{GUID}#TemplateName

No comments:

Post a Comment