Create Site Collections in SharePoint Online using CSOM

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.

12 Responses to “Create Site Collections in SharePoint Online using CSOM”

  1. Jedik Says:

    Hello, i can’t seem to find anywhere HelperToken.cs file.
    Please, help

  2. Jedik Says:

    Regarding last comment. I found it can be found in SharePoint 2013 Samples code.
    Here’s link –

    Thank’s for the nice post!

  3. Jedik Says:

    I belive that tenantAdminUri propery should be set to

    When i was calling -admin i kept receiving 400:error ,
    and after a while i tried the one i suggest i received context easily.

  4. SharePoint Online 2013 – Remotely create site collections (CSOM) | Danny Ong Says:

    […] SharePoint Online 2013 – Remotely create site collections (CSOM) […]

  5. Michael Says:

    @Jedik: You’re not correct.
    To be allowed to add sitecollections you need to be in the context of the admin site.
    It is import however, to have you’re web.config or app.config holding appropriate values for ClientID, ClientSecret and Realm (which is the TenantID for SharePoint Online, see: ).

    To get a TokenHelper class, just create a dummy SharePoint app in Visual Studio. It will be added to the project automatically.

  6. Shamil Says:

    I was wondering how this could be leveraged to automatically create mysites for a list of users populated in a CSV. For example, if I had 500 user accounts in a CSV, and I wanted to create their mysites before they signed into tenant.

  7. admin Says:

    Please have a look at this Office Dev Patterns and Practices example. Note that it is not recommended to submit more that 200 personal sites for Creation per request. So if you have a CSV with about 500 you should split that up in multiple requests.

  8. Creating SharePoint Site Collection through PowerShell CSOM | Share and PowerShell Says:

    […] is based on another blog post here: In that article the author shows how to use the CSOM with C# to create a Site Collection in Office […]

  9. Alex Brassington Says:

    Nice post. I’ve translated the Site Collection creation process into PowerShell here:

  10. Naveen Kumar Says:

    Request you to please let me know the code by which I can apply the custom site Template on the newly created site using .net client object model.

    Note: I have created a blank site collection using .net client object code as above. It is ok. And again I uploaded the my custom template wsp file to solution gallery of newly created site collection. And then I activated that. I done all the things by using .net client object model code. All the things are working fine.
    Now I have to apply the custom site template(wsp file uploaded to the solution gallery) by .net client object model code.

    Manually I can apply it without any error. But the requirement is to apply by code.

    Thanks and Regards,
    Naveen Kumar
    Mob: +91- 8800904241
    Email: (personal)
    Email (official)

  11. Frustrated Says:

    Quick question: When a subsequent lookup for the app id occurs to appinv.aspx, will the saved AppPermissionRequest XML remain and be visible? Looking for a response from anyone with knowledge.

  12. Johan Says:

    Hi Frustrated, you will not be able to see the Permission Request XML on subsequent lookups. That field works as an input field only.

Leave a Reply">Entries (RSS) and Comments (RSS).