How to Create a Modern Site Programmatically in SharePoint Office 365 using Patterns and Practice (OfficeDevPNP)


Sathish Nadarajan
SharePoint MVP
Published On :   02 May 2018
Visit Count
Today :  1    Total :   1370



The article explains about how to create a modern site in SharePoint Office 365 Programmatically. This will be helpful, during the provisioning of the sites. There are a lot of difference between the modern and the classic sites. Already some time back, we have seen how to create the Classic Sites programmatically HERE. But, the modern sites cannot be created in that way. It requires a minor change and we can have a look on it in this article.

Usually, when we create a site by SharePoint Admin Portal, we cannot see the templates of the Modern sites.

clip_image002

clip_image003

We cannot see the Modern site templates (Modern Team Site and Communication Site here). To do that, we need to go the below screen. https://<<TenantName>>.sharepoint.com/_layouts/15/sharepoint.aspx

clip_image005

This is a manual process and these sites will also be NOT listed on the site collections list.

https://<<Tenant>>-admin.sharepoint.com/_layouts/15/online/SiteCollections.aspx

clip_image007

We can see in the upcoming articles to get the list of Modern sites available in the tenant and how to delete them. As of now, let us see how to create the modern sites using C# CSOM PNP.

 public static async Task CreateCommunicationSite(string Password, string siteTitle)
         {
             string UserName = "sathish@*****.onmicrosoft.com";
             using (var clientContext = new ClientContext("https://********.sharepoint.com/"))
             {
                 SecureString passWord = new SecureString();
                 foreach (char c in Password.ToCharArray()) passWord.AppendChar(c);
                 clientContext.Credentials = new SharePointOnlineCredentials(UserName, passWord);
 
                 var commResults = await clientContext.CreateSiteAsync(new OfficeDevPnP.Core.Sites.CommunicationSiteCollectionCreationInformation()
                 {
                     Url = "https://*****.sharepoint.com/sites/" + siteTitle,
                     SiteDesign = OfficeDevPnP.Core.Sites.CommunicationSiteDesign.Topic,
                     Title = siteTitle,
                     Lcid = 1033
                 });
             }
         }
 
 

The above method will be called from the main method as below.

 SiteCollections.CreateCommunicationSite(password, "SiteTitle").Wait();

In the same manner, the Modern Team site can also be created.

This will be useful during the Provisioning exercise.

Happy Coding,

Sathish Nadarajan.

Categories