2

I'm trying to create a new site collection in Sharepoint Online tenant with Visual Studio.

1) I found http://msdn.microsoft.com/en-us/library/office/ms448720(v=office.14).aspx which says that the solution is available for Sharepoint Online

    SPWeb mySite = SPContext.Current.Web;
    SPWebCollection subSites = mySite.Webs;

However SPWeb are not recognized and "using Microsoft.SharePoint.SPContext" (http://msdn.microsoft.com/en-us/library/office/microsoft.sharepoint.spcontext(v=office.14).aspx) is not recognized.

2) I can create subsites for a given site collection using:

WebCreationInformation creation = new WebCreationInformation();
creation.Url = "Custom1";
creation.Title = "My Custom Site";
creation.WebTemplate = "CustomBlogTemplate";
context.Web.Webs.Add(creation);
context.ExecuteQuery();

but I don't see how I could extrapolate from that.

3) I tried using http://msdn.microsoft.com/en-us/library/office/ms462161(v=office.14).aspx and .Add() to the siteCollections

SPWebApplication webApplication = SPContext.Current.Site.WebApplication;
SPSiteCollection siteCollections = webApplication.Sites;

SPWebApplication and SpSiteCollection are not recognized and I hit a wall so many times that I have to ask is it really available for Sharepoint Online?
I have installed Sharepoint SDK and can list all the added references if needed.

Thank you in advance for help!

1

2 Answers 2

4

You will need the SharePoint Client Components SDK for this:

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("[email protected]", 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 = "[email protected]";                
                //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."); 
            }            
        }
    }
}

Code came from here.

2

You can create site collections in SharePoint Online with C# using the client side object model. You will need to install the SharePoint client SDK first which includes the CSOM libraries and the all important Microsoft.Online.SharePoint.Client.Tenant.dll.

There is a full walkthrough on how to get the client SDK and use the CSOM libraries with C# to create a new site collection in SharePoint Online here: http://social.technet.microsoft.com/wiki/contents/articles/24050.perform-basic-admin-operations-using-csom-in-sharepoint-online.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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