Creating Communication Sites in SharePoint Online Programatically

Back in August 2017, a brand new template for SharePoint sites called "Communication Sites" was rolled out to SharePoint Online. You can read more about communication sites here. Communication sites are a significant step forward for SharePoint sites, as they have a very modern look and feel right out of the box, and can easily serve a number of different business needs with very few customizations.

Ever since the communication sites were rolled out, I've seen that a lot of customers have been looking for a way to create/provision communication sites programmatically (PowerShell or CSOM). Earlier this month, Microsoft officially released guidance around how communication sites can be created using REST. For more information, see Creating SharePoint Communication Site using REST.

As you can see from the official documentation of the REST API that can be used to create communication sites, it lacks a working demonstration of how you can use the REST API to actually create a communication site. I've put together the following PowerShell script that can be used to provision a new Communication Site in SharePoint Online. The one thing to note is that you need a user's security context to create the communication site and that user will become the owner of the site. You cannot use an "App-Only" access token to create a site. You can easily convert the PowerShell script into any other programming language.

Note: You will need the SharePoint Online Client Components SDK to run the script.

Here is the script, enjoy!

[code lang="ps"]

Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
################################## Config Section Start ############################################
#The Url of the root SharePoint site for your tenant. Ensure there is no trailing slash at the end!
$rootUri = "https://yourtenant.sharepoint.com"
#The REST endpoint to create the communication site
$restEndPoint = $rootUri + "/_api/sitepages/communicationsite/create"
#Let's set the classification that we want to assign to the site.
$siteClass = "Contoso Confidential"
#There are 3 possible designs for communication sites. Use "null" for "Topic", "6142d2a0-63a5-4ba0-aede-d9fefca2c767" for Showcase and "f6cc5403-0d63-442e-96c0-285923709ffc" for a blank site
$designId = "6142d2a0-63a5-4ba0-aede-d9fefca2c767"
$siteTitle = "My Comm Site PowerShell2"
$siteDesc = "This is the description of my communications site"
$siteUrl = "https://yourtenant.sharepoint.com/sites/mycomm55"
$siteLCID = "1033"
############################### Config Section End ################################################
#Get the credentials from the user
if ($creds -eq $null)
{
$creds = Get-Credential
}
#Get the user context
$context = New-Object Microsoft.SharePoint.Client.ClientContext($rootUri)
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($creds.UserName, $creds.Password)
$context.ExecuteQuery()
#Get the Auth cookie
$authCookie = $context.Credentials.GetAuthenticationCookie($rootUri, $true)
#Get the Form Digest
$digest = $context.GetFormDigestDirect()
#Now let's build the REST web request
$contentType = 'application/json;odata=verbose'
$headers = @{}
$headers["Accept"] = "application/json;odata=verbose"
$headers["X-RequestDigest"] = $digest.DigestValue
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$session.Cookies.SetCookies($rootUri, $authCookie)
$body = @{
request = @{
__metadata = @{type ="SP.Publishing.CommunicationSiteCreationRequest"}
AllowFileSharingForGuestUsers = 'false'
Classification = $siteClass
Description = $siteDesc
SiteDesignId = $designId
Title = $siteTitle
Url = $siteUrl
lcid = $siteLCID
}
}
$jsonBody = ConvertTo-Json $body
$result = $null
#Shoot the request to the cloud!
$result = Invoke-RestMethod -Method Post -ContentType $contentType -Headers $headers -Uri $restEndPoint -Body $jsonBody -WebSession $session
if ($result -ne $null -and $result.d.Create.SiteStatus -eq 2)
{
Write-Output "Site created successfully at "$result.d.Create.SiteUrl
}
else
{
Write-Host "Failed to create site! Grab a fiddler trace while reproducing the failure to learn more about the failure!"
}

Comments

  • Anonymous
    March 20, 2018
    Code worked perfectly and is a very useful. Appreciate the effort and the sharing..!