I would like to create for sharepoint on premise a site collection remotely.

However, the only piece of code I found on internet is for sharepoint online https://gist.github.com/vman/9342224

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("admin@yoursite.onmicrosoft.com", 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";

            //Login name of Owner
            siteCreationProperties.Owner = "admin@yoursite.onmicrosoft.com";

            //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);


            //We will need the IsComplete property to check if the provisioning of the Site Collection is complete.
            tenantContext.Load(spo, i => i.IsComplete);


            //Check if provisioning of the SiteCollection is complete.
            while (!spo.IsComplete)
                //Wait for 30 seconds and then try again

            Console.WriteLine("SiteCollection Created.");



How can I tweak the above code to make it work on sharepoint 2013 et 2016



The above code you have mentioned is a client side code and it should work with SharePoint 2013 and 2016 on premise. Only thing here is instead on onlinecredential part you need to use Network credential.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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