Tuesday, 4 March 2014

Create Site Collections with CSOM in SharePoint Online

With the SharePoint Conference 2014, a lot of code samples and articles where recently released. I was most impressed by the Office App Model Samples found here: http://officeams.codeplex.com/

Within that, there is some really good code of which my favorite is Creating Site Collections in SharePoint Online with CSOM. Previously this was only possible via Powershell and the SharePoint Online (SPO) Commandlets. I have modified the code provided in the samples to make it a bit less complex and you can also use it outside of an app e.g. in console applications.

You will need the following DLLs for this code to work:
Microsoft.SharePoint.Client.dll
Microsoft.SharePoint.Client.Runtime.dll
Microsoft.Online.SharePoint.Client.Tenant.dll

The first two assemblies can be found in the ISAPI folder of your SharePoint 2013 Server Box. The Microsoft.Online.SharePoint.Client.Tenant.dll is a part of  the SharePoint Server 2013 Client Components SDK which can downloaded from here: http://www.microsoft.com/en-in/download/details.aspx?id=35585

So without much further ado, here is the code:

using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.SharePoint.Client;
using System;
using System.Security;
namespace CreateSiteCollections
{
class Program
{
static void Main(string[] args)
{
//Open the Tenant Administration Context with the Tenant Admin Url
using (ClientContext tenantContext = new ClientContext("https://yoursite-admin.sharepoint.com/"))
{
//Authenticate with a Tenant Administrator
SecureString passWord = new SecureString();
foreach (char c in "password".ToCharArray()) passWord.AppendChar(c);
tenantContext.Credentials = new SharePointOnlineCredentials("admin@yoursite.onmicrosoft.com", passWord);
var tenant = new Tenant(tenantContext);
//Properties of the New SiteCollection
var siteCreationProperties = new SiteCreationProperties();
//New SiteCollection Url
siteCreationProperties.Url = "https://yoursite.sharepoint.com/sites/codesite";
//Title of the Root Site
siteCreationProperties.Title = "Site Created from Code";
//Login name of Owner
siteCreationProperties.Owner = "admin@yoursite.onmicrosoft.com";
//Template of the Root Site. Using Team Site for now.
siteCreationProperties.Template = "STS#0";
//Storage Limit in MB
siteCreationProperties.StorageMaximumLevel = 100;
//UserCode Resource Points Allowed
siteCreationProperties.UserCodeMaximumLevel = 50;
//Create the SiteCollection
SpoOperation spo = tenant.CreateSite(siteCreationProperties);
tenantContext.Load(tenant);
//We will need the IsComplete property to check if the provisioning of the Site Collection is complete.
tenantContext.Load(spo, i => i.IsComplete);
tenantContext.ExecuteQuery();
//Check if provisioning of the SiteCollection is complete.
while (!spo.IsComplete)
{
//Wait for 30 seconds and then try again
System.Threading.Thread.Sleep(30000);
spo.RefreshLoad();
tenantContext.ExecuteQuery();
}
Console.WriteLine("SiteCollection Created.");
}
}
}
}

10 comments:

Geetanjali Arora said...

Great post as always :) Always some new learning when I go through your blogs mate. Keep writing good stuff.

manish singh said...

Hi,
I am not able to access the online site
I have done same thing as you have shown in your code.

on clientcontx.ExecuteQuery();
I am getting the error below

An unhandled exception of type 'System.Net.WebException' occurred in System.dll

Additional information: The remote server returned an error: (407) Proxy Authentication Required.


Please let me know what the other things i have to do.

KS said...

Hello Vardhaman,
Nice article!

I wanted to create root site collection i.e. https://yoursite.sharepoint.com/

using powershell. do you know any way.
It tried different way but was it is saying that root site collection doesn't exists.

Thanks in advance!

Unknown said...

Hi,

I keep getting correlation-id errors on all lists on the newly created sites. I am using the 64-bit 15 or 16 version dlls. Both versions give me the same correlation-id errors on lists. Anyone any idea what is causing this issue?

Surendra Reddy said...

Great post and very useful concept to create SharePoint site collection.

Santhosh said...

Nice,helpful

Unknown said...

How is tenant.CreateSite different from New-SPOSite powershell cmdlet?

Unknown said...

What if I wanted to use custom template to create a site collection.

Is there any way that I can upload my custom template to Central admin solution gallery

Much appreciated if you can respond.

Thanks.

Vardhaman Deshpande said...

Hi Ratnakumar,

To apply a custom template, you will first create a template using the PnP Schema. Then create a SPO site collection using an OOB template and then apply your PnP template to the site collection.

Hope this helps.

Unknown said...

I want to create one document library, set content search web parts on new created site collection home page. is any idea