Monday 22 December 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:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
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";
 
//Email 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.");
 
}
}
}
}

No comments:

Post a Comment