How to create Team Site in SharePoint Online using JSOM/REST API in SharePoint hosted app?
3 Answers
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
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.
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– Raj2017-10-31 05:47:03 +00:00Commented Oct 31, 2017 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– Akshay2017-10-31 06:35:38 +00:00Commented Oct 31, 2017 at 6:35