1

How to create Team Site in SharePoint Online using JSOM/REST API in SharePoint hosted app?

  • Hi Raj, Do you want to create sub site or Site Collection? – Shantha Kumar Thambidurai Oct 30 '17 at 15:55
  • I need to create Site Collection – Raj Oct 31 '17 at 5:46
1

By using Powershell you can create

New-SPSite http://www.contoso.com -OwnerAlias "DOMAIN\jdow" -Name "Contoso" -Template "STS#0"

Reference: you can get Site Template ID List for SharePoint

Using JSOM

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

namespace Console.Office365
{
 class Program
 {
     static void Main(string[] args)
     {
         //Sample();
         CreateSiteCollection();
     }

     public static void CreateSiteCollection()
     {
         string TenantURL = "https://******-admin.sharepoint.com";
         string Title = "AASathish1";
         string Url = "https://********.sharepoint.com/sites/AASathish3";
         string UserName = "Sathish@*******.onmicrosoft.com";
         string Password = "******";

         //Open the Tenant Administration Context with the Tenant Admin Url
         using (ClientContext tenantContext = new ClientContext(TenantURL))
         {
             //Authenticate with a Tenant Administrator
             SecureString passWord = new SecureString();
             foreach (char c in Password.ToCharArray()) passWord.AppendChar(c);
             tenantContext.Credentials = new SharePointOnlineCredentials(UserName, passWord);

             var tenant = new Tenant(tenantContext);

             //Properties of the New SiteCollection
             var siteCreationProperties = new SiteCreationProperties();

             //New SiteCollection Url
             siteCreationProperties.Url = Url;

             //Title of the Root Site
             siteCreationProperties.Title = Title;

             //Email of Owner
             siteCreationProperties.Owner = UserName;

             //Template of the Root Site using Team Site.
             siteCreationProperties.Template = "STS#0";

             //Storage Limit in MB
             siteCreationProperties.StorageMaximumLevel = 100;

             //UserCode Resource Points Allowed
             siteCreationProperties.UserCodeMaximumLevel = 200;

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

             System.Console.WriteLine("SiteCollection Created.");

         }


     }
  }
 }

Reference: Programmatically creating Sites and Site Collections from a Custom Web Template

0

To remotely create Site Collections in a SharePoint Online tenant is only possible by using the SharePoint Online Management Shell or the CSOM library.

It's not possible with the JSOM library.

0

You can use bellow code to create site in SharePoint online:

 $.ajax({
        url: "/_api/web/webinfos/add",
        type: "POST",
        headers: {
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        data: JSON.stringify({
            'parameters': {
                '__metadata': {
                    'type': 'SP.WebInfoCreationInformation'
                },
                'Url': <New SubSite URL>,
                'Title': <New SubSite Title>,
                'Description': 'Test',
                'Language': 1033,
                'WebTemplate': 'sts',
                'UseUniquePermissions': true
            }
        })           
    });
  • This code will create subsite and I need to create Site Collection. – Raj Oct 31 '17 at 5:47
  • It is possible using server side code, so you need to create provider hosted app and pass parameters for site collection creation and create site collection on button click by calling provider hosted app. – Akshay Oct 31 '17 at 6:35

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.