Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have been trying out to create site collection using CSOM but unable to fins and class which creates site collection.

I have read in MSDN that CSOM runs as site collection scope hence creating site collection permission is not available.

Sub-Site i am able to create properly but unable to create site collection.

share|improve this question

2 Answers 2

This example is taken from a SharePoint Online environment with provided hosted apps in mind. Creating a console application, adding reference to

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

and pasting this snippet to the main method should do the trick.

var SHAREPOINT_PID = "00000003-0000-0ff1-ce00-000000000000"; // SharePoint constant
var tenantAdminUri = new Uri("https://<TENANT>-admin.sharepoint.com");
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>.sharepoint.com/sites/site1",
        Owner = "<USER>@<TENANT>.onmicrosoft.com",
        Template = "STS#0",
        StorageMaximumLevel = 1000,
        UserCodeMaximumLevel = 300
    };
    tenant.CreateSite(properties);

    context.Load(tenant);
    context.ExecuteQuery();
}

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.

Reference: Create Site Collections in SharePoint Online using CSOM

share|improve this answer

Tenant.CreateSite method queues a site collection for creation with the specified properties. Tenant class is a part of Microsoft.Online.SharePoint.Client.Tenant.dll (SharePoint Online Management Shell)

How to create site collection via CSOM in SharePoint 2013

using System;
using System.Security;
using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.SharePoint.Client;

namespace CSOMProvisioning
{
    class Program
    {
        static void Main(string[] args)
        {

            const string username = "username@contoso.onmicrosoft.com";
            const string password = "password";
            const string tenantAdminUrl = "https://contoso-admin.sharepoint.com/";
            var securedPassword = new SecureString();
            foreach (var c in password.ToCharArray()) securedPassword.AppendChar(c);
            var credentials = new SharePointOnlineCredentials(username, securedPassword);



            using (var context = new ClientContext(tenantAdminUrl))
            {
                context.Credentials = credentials;
                CreateSite(context, "https://contoso.sharepoint.com/sites/marketing","Marketing");
            }

        }


        /// <summary>
        /// Create a new site.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="url">rootsite + "/" + managedPath + "/" + sitename: e.g. "https://auto.contoso.com/sites/site1"</param>
        /// <param name="title">site title: e.g. "Test Site"</param>
        /// <param name="owner">site owner: e.g. admin@contoso.com</param>
        /// <param name="template">The site template used to create this new site</param>
        /// <param name="localeId"></param>
        /// <param name="compatibilityLevel"></param>
        /// <param name="storageQuota"></param>
        /// <param name="resourceQuota"></param>
        /// <param name="timeZoneId"></param>
        internal static void CreateSite(ClientContext context, String url, String owner, String title =null, String template = null, uint? localeId = null, int? compatibilityLevel = null, long? storageQuota = null, double? resourceQuota = null, int? timeZoneId = null)
        {
            var tenant = new Tenant(context);

            if (url == null)
                throw new ArgumentException("Site Url must be specified");

            if (string.IsNullOrEmpty(owner))
                throw new ArgumentException("Site Owner must be specified");

            var siteCreationProperties = new SiteCreationProperties {Url = url, Owner = owner};
            if (!string.IsNullOrEmpty(template))
                siteCreationProperties.Template = template;
            if (!string.IsNullOrEmpty(title))
                siteCreationProperties.Title = title;
            if (localeId.HasValue)
                siteCreationProperties.Lcid = localeId.Value;
            if (compatibilityLevel.HasValue)
                siteCreationProperties.CompatibilityLevel = compatibilityLevel.Value;
            if (storageQuota.HasValue)
                siteCreationProperties.StorageMaximumLevel = storageQuota.Value;
            if (resourceQuota.HasValue)
                siteCreationProperties.UserCodeMaximumLevel = resourceQuota.Value;
            if (timeZoneId.HasValue)
                siteCreationProperties.TimeZoneId = timeZoneId.Value;
            var siteOp = tenant.CreateSite(siteCreationProperties);
            context.Load(siteOp);
            context.ExecuteQuery();

        }



    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.