Creating a subsite using the client side object model in SharePoint 2010

We’ve all seen loads of things to you can so with the new Client Side Object Model in SharePoint 2010. I’m personally loving it more and more! The only downside is, most examples and code is made to retrieve information. The object model can also be used to create things!

Here’s a quick post on how we are creating subsites using the client side object model:

using (ClientContext ctx = new ClientContext("http://yoursite/"))
                WebCreationInformation wci = new WebCreationInformation();
                wci.Url = "YourSite"; // Relative URL
                wci.Title = "YourSite";
                wci.Description = "Your nice description.";
                wci.UseSamePermissionsAsParentSite = true;
                wci.WebTemplate = "STS#0";
                wci.Language = 1033; //LCID

                Web w = ctx.Site.RootWeb.Webs.Add(wci);

And that’s it :)

The WebTemplate might be a bit odd, check this PowerShell script to to retrieve the correct values you can enter. Also note, custom web templates are possible and are also retrieved using this PowerShell:

PS C:\Users\ruarco> $s = Get-SPSite("http://yoursite/");
PS C:\Users\ruarco> $s.GetWebtemplates(1033) | select Name, Title

Note the LCID in the GetWebtemplates. The select is handy because custom web templates get a long guid and you’ll only want to select Name at those things.