Creating SPWeb’s using Client Object Model (CSOM)


This is a quick example of how to use the JavaScript client object model to create a new SPWeb. As a bonus, this also covers adding it to the root of the site collection. Now, remember  in SP2013 if you are doing this from an app you will need to request rights to either the hosting SPWeb of your app or the hosting SPSite. Otherwise you’ll just get access denied errors.

//get client context and root web
var clientContext = new SP.ClientContext.get_current();
var rootWeb = clientContext.site.rootWeb();
this.clientContext.load(rootWeb);
this.clientContext.executeQuery();


//set creation info
var WCI = new SP.WebCreationInformation();
WCI.set_webTemplate('TemplateName');
WCI.set_description('my app web');
WCI.set_title('My App Web Title');
WCI.set_url(url);
WCI.set_language(1033);
this.rootWeb.get_webs().add(WCI);
this.rootWeb.update();

//save changes and generate callbacks
this.clientContext.load(this.rootWeb);
this.clientContext.executeQueryAsync(Function.createDelegate(this, this.onCreationSuccess), Function.createDelegate(this, this.Error);

  1. No comments yet.
(will not be published)


%d bloggers like this: