1

Hi I've succeeded creating document libraries and folders but I'm having trouble creating a (communication) site in SharePoint online. Thanks for the help!

  • Please elaborate your problem in more words and also provide code that you tried so far so people can understand your problem in more better way to help you out!! – Dikesh Gandhi Jan 15 at 4:36
1
// Starting with ClientContext, the constructor requires a URL to the
// server running SharePoint.
ClientContext context = new ClientContext("http://SiteUrl");

WebCreationInformation creation = new WebCreationInformation();
creation.Url = "web1";
creation.Title = "Hello web1";
Web newWeb = context.Web.Webs.Add(creation);

// Retrieve the new web information.
context.Load(newWeb, w => w.Title);
context.ExecuteQuery();

label1.Text = newWeb.Title;

source: https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-client-library-code

0

You could use the PNP CSOM component to help you to achieve that.

Link to Nuget PnP CSOM Core component - link to Nuget Package

var targetTenantUrl = "https://[tenant].sharepoint.com/";

using (var context = new ClientContext(targetTenantUrl))
{
    context.Credentials = OfficeDevPnP.Core.Utilities.CredentialManager.GetSharePointOnlineCredential("[Name-of-Your-Credentials]");

    // Create new "modern" communication site at the url https://[tenant].sharepoint.com/sites/mymoderncommunicationsite
    var communicationContext = await context.CreateSiteAsync(new CommunicationSiteCollectionCreationInformation {
        Title = "title", // Mandatory
        Description = "description", // Mandatory
        Lcid = 1033, // Mandatory
        AllowFileSharingForGuestUsers = false, // Optional
        Classification = "classification", // Optional
        SiteDesign = CommunicationSiteDesign.Topic, // Mandatory
        Url = "https://[tenant].sharepoint.com/sites/mymoderncommunicationsite", // Mandatory
    });
    communicationContext.Load(communicationContext.Web, w => w.Url);
    communicationContext.ExecuteQueryRetry();
    Console.WriteLine(communicationContext.Web.Url);
}

Note that the property SiteDesign "CommunicationSiteDesign" could be

  • CommunicationSiteDesign.Topic
  • CommunicationSiteDesign.Showcase
  • CommunicationSiteDesign.Blank

More information here

0

The recommended way to create communication and modern team site in modern SharePoint online is using PnP PowerShell command, its very sample and user friendly:

Below is the PnP PowerShell command to create communication site in SharePoint online:

New-PnPSite -Type CommunicationSite -Title Contoso -Url https://tenant.sharepoint.com/sites/contoso -SiteDesign Showcase

Notes:

SiteDesign is enum type, we can pass any of the below:

  • Topic
  • Showcase
  • Blank

For more details please refer the below Microsoft article:

New-PnPSite

Prerequisites to execute the PnP PowerShell:

In order to execute the PnP PowerShell command, we need to install the "PnP PowerShell", please refer the below article to install PnP PowerShell:

Office 365: Getting started with SharePoint PnP PowerShell – installation

Nevertheless, we can create communication site using C# coding as well, Microsoft has provided all the options we need.

C# coding to create communication site:

// Let's use the CreateSiteAsync extension method of PnP CSOM Core
// to create the "modern" team site

var targetTenantUrl = "https://[tenant].sharepoint.com/";

using (var context = new ClientContext(targetTenantUrl))
{
    context.Credentials = OfficeDevPnP.Core.Utilities.CredentialManager.GetSharePointOnlineCredential("[Name-of-Your-Credentials]");

    // Create new "modern" communication site at the url https://[tenant].sharepoint.com/sites/mymoderncommunicationsite
    var communicationContext = await context.CreateSiteAsync(new CommunicationSiteCollectionCreationInformation {
        Title = "title", // Mandatory
        Description = "description", // Mandatory
        Lcid = 1033, // Mandatory
        AllowFileSharingForGuestUsers = false, // Optional
        Classification = "classification", // Optional
        SiteDesign = CommunicationSiteDesign.Topic, // Mandatory
        Url = "https://[tenant].sharepoint.com/sites/mymoderncommunicationsite", // Mandatory
    });
    communicationContext.Load(communicationContext.Web, w => w.Url);
    communicationContext.ExecuteQueryRetry();
    Console.WriteLine(communicationContext.Web.Url);
}

For more details, please refer the below article:

Provisioning "modern" team sites programmatically

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