Archive for February, 2013

Create Site Collections in SharePoint Online using CSOM

Tuesday, February 26th, 2013

This week I’m working with a customer that wants their users to be able to create site collections in SharePoint Online. Below is a step-by-step guide how this can be done using the Client Side Object Model in SharePoint 2013.

UPDATE 2014-01-15: Registering a


The example application is created on my Windows 8 desktop computer without SharePoint 2013 installed. The following applications are required:

  • Visual Studio 2012
  • Microsoft Office Developer Tools for Visual Studio 2012
  • SharePoint Online Management Shell

Create a Client Application

In this example I’ve created a simple console application but it might as well be a service running in Azure or any other .NET application. Start by creating a Windows console application and add assembly references to:

  • Microsoft.SharePoint.Client.dll
  • Microsoft.SharePoint.Client.Runtime.dll
  • Microsoft.Online.SharePoint.Client.Tenant.dll

The assemblies can be found here: C:Program FilesSharePoint Online Management ShellMicrosoft.Online.SharePoint.PowerShell.

Add TokenHelper Class

To make it easier for developers Microsoft has created a helper class used for OAuth communication with SharePoint. This example aslo uses this class and it can be included by creating a new App for SharePoint (provider or autohosted) in Visual Studio project and then copying the TokenHelper.cs file into the console application. Change the namespace in the class to match the console applications namespace. The class requires references to the following assemblies:

  • System.Web.dll
  • System.ServiceModel.dll
  • System.IdentityModel.dll
  • System.Web.Extensions.dll
  • Microsoft.IdentityModel.dll
  • Microsoft.IdentityModel.Extensions.dll

Add the Code

Start by adding these two using statements to Program.cs:

[code lang=”csharp”]
using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.SharePoint.Client;

Then simply add the following code inside the Main-method:

[code lang=”csharp”]

var SHAREPOINT_PID = "00000003-0000-0ff1-ce00-000000000000"; // SharePoint constant
var tenantAdminUri = new Uri("https://<TENANT>");
var token = TokenHelper.GetAppOnlyAccessToken(SHAREPOINT_PID, tenantAdminUri.Authority, null).AccessToken;

using (var context = TokenHelper.GetClientContextWithAccessToken(tenantAdminUri.ToString(), token))
var tenant = new Tenant(context);
var properties = new SiteCreationProperties()
Url = "https://<TENANT>",
Owner = "<USER>@<TENANT>",
Template = "STS#0",
StorageMaximumLevel = 1000,
UserCodeMaximumLevel = 300



All occurences of <TENANT> should be replaced with your real tenant name and also <USER> should be replaced with the username of whoever is going to be the owner of the site collection.

Register the Application in SharePoint Online

For this to work the application must be registered in your Office 365 tenant and also given tenant permissions. To do this first navigate to https://<TENANT> Click on the buttons to generate an App Id and an App Secret. Fill in a Title, App Domain and Redirect URI and click on the Create button. You will be taken to a summery page. Copy the App Id and App Secret values because you will be needing them later.

Next you need to give the application tenancy permission. This can be done by going to https://<TENTANT> Fill in your App Id and hit Lookup. Now paste the following XML in Permission Request XML and click Create:

[code lang=”xml”]
<AppPermissionRequests AllowAppOnlyPolicy="true">
<AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" />

You will be taken to a summary page saying that this application requires full control of all site collections, access basic user information and share its permission with other users. Click Trust it.

Finally you need to find out the app realm. Navigate to https://<TENANT> Identify the row with your registered application and look at the App Identifier column. The unique identifier after the @-sign in the App Identifier is the app realm. Copy the realm (not the whole App Identifier just the last part after the @-sign).

Finish the Client Application

The App Id, App Secret and App Realm now needs to be added to the client application. If your console application does not have an app.config add one and then add these appSettings inside the configuration element:

[code lang=”xml”]
<add key="ClientId" value="<APP ID>"/>
<add key="ClientSecret" value="<APP SECRET>"/>
<add key="Realm" value="<APP REALM>"/>

Replace , <APP ID>, <APP SECRET> and < APP REALM> with the values you got from SharePoint Online. The application should now be ready for testing. Not many lines of code but some wiring up to make it work with OAuth and SharePoint Online.">Entries (RSS) and Comments (RSS).