Create Modern Sites in SharePoint Online using CSOM
-
06-01-2021 - |
Pergunta
Can we create Modern Team site and Communication sites using CSOM in SharePoint Online ?
I tried to create the modern site for GROUP#0 and SITEPAGEPUBLISHING#0 with the below code sample and getting the error
The web template SITEPAGEPUBLISHING#0 is not available for sites on this tenant.
string tenantadminurl = tenantAdminUrl.Replace("-admin", "").Trim() + "/sites/";
// TemplateName = "Modern Team Site", TemplateID = "GROUP#0"
// TemplateName = "Modern Communication", TemplateID = "SITEPAGEPUBLISHING#0"
//string siteurl = tenantadminurl + objSiteCollectionDetails.SiteTitle;
// string tenanturl = tenantAdminUrl.Replace("-admin", "").Trim();
string siteurl = "https://tarundev-admin.sharepoint.com" + "/sites/"+"SampleMDsite";
// string siteurl = "https://tarundev.sharepoint.com" + "/sites/"+"SampleMDsite";
objSiteProperties.SiteUrl = siteurl;
var tenant = new Tenant(context);
var properties = new SiteCreationProperties()
{
CompatibilityLevel = 15,
Title = objSiteCollectionDetails.SiteTitle,
Url = siteurl,
Owner = owner,
Template = objSiteCollectionDetails.SiteTemplate,
StorageMaximumLevel = 1,
UserCodeMaximumLevel = 300
};
tenant.CreateSite(properties);
context.ExecuteQuery();
Can anyone guide me in the right way.
Solução
This is not possible currently using OOTB SPO CSOM.
However, using the latest PnP Core and PnP PowerShell components, you will be able to create them programmatically.
Using CSOM C#
You can use the below code to create Communication site:
CommunicationSiteCollectionCreationInformation communicationSiteInfo = new CommunicationSiteCollectionCreationInformation
{
Title = "Test Communication Site",
Url = "https://tenantname.sharepoint.com/sites/TestCommunicationSite",
//possible SiteDesign values can be
//SiteDesign = CommunicationSiteDesign.Showcase,
//SiteDesign = CommunicationSiteDesign.Blank,
SiteDesign = CommunicationSiteDesign.Topic,
Description = "Test modern communication site description",
Owner = "user@tenant.onmicrosoft.com"
AllowFileSharingForGuestUsers = true,
//Classification = "HR",
//Lcid = 1033
};
var createCommSite = await context.CreateSiteAsync(communicationSiteInfo);
To create a Modern team site, you can use the below code:
TeamSiteCollectionCreationInformation modernteamSiteInfo = new TeamSiteCollectionCreationInformation
{
Description = "Test modern teamsite description",
DisplayName = "Test Modern Team Site",
Alias = "TestModernTeamSite",
IsPublic = true,
//Classification="IT"
};
var createModernSite = await context.CreateSiteAsync(modernteamSiteInfo);
These are currently in beta and may not always work as expected.
To install the latest PnP core, go to your Project References > Manage nuget packages
.
Download the SharePointPnPCoreOnline
component. Its version should be 2.20.1711
or higher (i.e Nov 2017 or higher).
You also need to add the statement using OfficeDevPnP.Core.Sites;
in your class.
To create "Modern" unified team site(with associated Azure AD group) , you can also follow the approach that I have mentioned here which requires access to Azure portal.
Using PnP PowerShell
To provision these site programmatically using PowerShell, you can use the PnP PowerShell
commandlets mentioned below. Download the latest PnP PowerShell (November 2017) or higher)
so that these commands are available.
For Communication site -
New-PnPSite -Type CommunicationSite -Title "Test Communication Site"
-Url "https://tenant.sharepoint.com/sites/testCommunicationSite"
-SiteDesign Showcase -AllowFileSharingForGuestUsers
For Modern team site -
New-PnPSite -Type TeamSite -Title "Test ModernTeam Site" -Alias "TestModernTeamSite" -IsPublic
Reference - New-PnPSite
Download link - PnP PowerShell