Create Modern Communication Site Using PnP CSOM

Communication Sites and Modern Team Sites are great additions to SharePoint Online. They enable content authors/users to create/manage their sites more easily than before. A user with minimum knowledge of SharePoint can create a site, wiki, how to page, publish articles, create charts, add images, videos, links, and documents with greater ease than before.
 
Please refer to the below MSDN Wiki links for more information on Modern Team and Communication Sites.
PNP Core CSOM documentation can be found on the official site here. PnP Core CSOM library packages can be downloaded here.
 
For Creating the Modern Communication Site we have to use the CommunicationSiteCollectionCreationInformation Class with the below Properties,
 
Name
Description
AllowFileSharingForGuestUsers
If set to true, file sharing for guest users will be allowed.
Classification
The Site classification to use. For instance 'Contoso Classified'.
Description
The description to use for the site.
Lcid
The language to use for the site. If not specified will default to the language setting of the clientcontext.
Owner
The owner of the site. Reserved for future use.
SiteDesign
The built-in site design to used. If both SiteDesignId and SiteDesign have been specified, the GUID specified as SiteDesignId will be used.
SiteDesignId
The Guid of the site design to be used. If specified will override the SiteDesign property
Title
The title of the site to create
Url
The fully qualified url of the site.
 
Step 1
 
Create a C# console application.
 
Step 2
 
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).
 
 
Step 3
 
Include the below namespaces.
  1. using Microsoft.SharePoint.Client;  
  2. using OfficeDevPnP.Core.Sites;  
  3. using System;  
  4. using System.Linq;  
  5. using System.Security;  
Declare the variables for admin user, tenant url and password.
  1. public static string TenantUrl = "https://tenant-admin.sharepoint.com/";  
  2. public static string username = "[email protected]";  
  3. public static string password = "********";  
Step 4
 
Create one static method for creating the Modern Communication site and call the method on  Main Functions. The complete Code is as below.
  1. static void Main(string[] args) {  
  2.     Program.CreateModernCommunicationSite();  
  3.     Console.Read();  
  4. }  
  5. public static async void CreateModernCommunicationSite() {  
  6.     try {  
  7.         SecureString secureString = new SecureString();  
  8.         password.ToList().ForEach(secureString.AppendChar);  
  9.         var credentials = new SharePointOnlineCredentials(username, secureString);  
  10.         using(ClientContext context = new ClientContext(TenantUrl)) {  
  11.             context.Credentials = credentials;  
  12.             CommunicationSiteCollectionCreationInformation communicationSiteInfo = new CommunicationSiteCollectionCreationInformation {  
  13.                 Title = "C Sharp Corner Demo Communication Site",  
  14.                     Url = "https://saketadev.sharepoint.com/sites/CSharpDemoCommunicationSite",  
  15.                     //possible SiteDesign values can be CommunicationSiteDesign.Showcase, CommunicationSiteDesign.Blank,  
  16.                     SiteDesign = CommunicationSiteDesign.Topic,  
  17.                     Description = "Demo Description",  
  18.                     Owner = username,  
  19.                     AllowFileSharingForGuestUsers = true,  
  20.                     //Classification = "HR",  
  21.                     //Lcid = 1033  
  22.             };  
  23.             var createCommSite = await context.CreateSiteAsync(communicationSiteInfo);  
  24.             Task.WaitAll();  
  25.         }  
  26.     } catch (Exception ex) {  
  27.         Console.Write(ex.Message);  
  28.     }  
  29. }  
Step 5
 
After executing the console app, the communiciation site will be created as below.