9

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.

1 Answer 1

13

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).

enter image description here

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

6
  • Your solutions worked for one day after that site is not creating. No exception and no output. What may be the reason. Because of Beta Version
    – TARUN
    Nov 23, 2017 at 7:49
  • yes beta version. I would suggest that you wait for couple of days for this to be stable. Nov 23, 2017 at 8:28
  • What is the context required to create the modern site either tenant( tenant.sharepoint.com ) or tenantadmin (tenant-admin.sharepoint.com )
    – TARUN
    Nov 23, 2017 at 8:56
  • The context required is https://tenant.sharepoint.com/sites/testSite. I used a global admin account, but sometime back tested with a normal account and was able to create site collection. Like I said,this is beta, so I guess they are allowing "everyone" to create site collection :( Nov 23, 2017 at 9:01
  • "App-Only is currently not supported." This error i am getting while creating the Modern Team Site..
    – TARUN
    Dec 4, 2017 at 7:23

Your Answer

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

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