Create a SharePoint Site using Client Object Model C#

Required namespace:-
using Microsoft.SharePoint.Client;using Microsoft.Online.SharePoint.TenantAdministration; 

Following is the full code sample for creating a new sitecollection in SharePoint using CSOM.

bool rootSiteExist;
SP.ClientContext ctx = new ClientContext(siteUrl);
ctx.Credentials = new SharePointOnlineCredentials(userName, passWord);
this.ctx.Load(this.ctx.Web);

try
{
    ctx.ExecuteQuery();
    rootSiteExist = true;
}
catch (Exception)
{
    //Indicates the given sub site is missing
    //This block will be executed when the given Web is not found on the server
    
    rootSiteExist = false;
}

//Create a Site if there is no existing site with the same url.
if(!rootSiteExist)
{
    string targetURL = "https://-admin.sharepoint.com";
    SP.ClientContext adminCtx = new SP.ClientContext(siteUrl);
    adminCtx.Credentials = new SharePointOnlineCredentials(userName, passWord);

    Tenant tenant = new Tenant(context);
    adminCtx.Load(tenant, t => t.ResourceQuota, t => t.ResourceQuotaAllocated, t => t.StorageQuota, t => t.StorageQuotaAllocated);
    adminCtx.ExecuteQuery();

    double avaialableResource = tenant.ResourceQuota - tenant.ResourceQuotaAllocated;
    long availableStorageResource = tenant.StorageQuota - tenant.StorageQuotaAllocated;

    long storageMaximumLevel = 1000; //say
    double userCodeMaximumLevel = 300 //say

    if (availableStorageResource >= storageMaximumLevel)
    {
        if (avaialableResource >= userCodeMaximumLevel)
        {
            bool retryAttemted = false;
            while (true)
            {
                var properties = new SiteCreationProperties()
                {
                    CompatibilityLevel = compatibilityLevel,
                    Url = siteUrl,
                    Owner = owner,
                    Template = template,
                    StorageMaximumLevel = storageMaximumLevel,
                    UserCodeMaximumLevel = userCodeMaximumLevel,

                };
                tenant.CreateSite(properties);
                adminCtx.Load(tenant);

                try
                {
                    /*If you get an error here it means, a root site with the same url is lying in the RecycleBin of the admin Site Collection. In that case
you have to first remove the deleted site before proceeding.*/
                    
                    adminCtx.ExecuteQuery();
                    break;
                }
                catch (SP.ServerException)
                {
                    if (retryAttemted)
                        throw;

                    tenant.RemoveDeletedSite(siteUrl);
                    connect.adminCtx.ExecuteQuery();
                    retryAttemted = true;
                }
            }

            /*Now trying to connect to the newly cretaed Site with the given Url. The site may not show up immediately, hence constantly trying to connect to the site after a given time interval (wait period).*/
            
            int retryCount = 0;
            SP.Web web = null;
            
            while (true)
            {
                try
                {
                    ctx = new ClientContext(siteUrl);
                    ctx.Credentials = new SharePointOnlineCredentials(userName, passWord);

                    web = this.ctx.Web;
                    this.ctx.Load(web);
                    this.ctx.ExecuteQuery();
                    
                    break;
                }
                catch (Exception)
                {
                    ++retryCount;
                    if (retryCount <= 20)
                    {
                        Thread.Sleep(200000);
                        continue;
                    }
                    break;
                }
            }
        }
        else
        {
            //Resources are inadequate
        }
    }else    
    {
        //Storage Size is inadequate
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s