Working with Communication sites Using PowerShell

A SharePoint communication site is a great modern design site that can be used to share news, reports, and other information in a visually compelling format and uses adoptive layout. More information can be found in this Microsoft article: What is a SharePoint communication site?

The template used to create a Communication SharePoint site is SITEPAGEPUBLISHING#0.

The following code will create a Communication Sites using the SharePoint Online Management Shell. A connection to SPO tenant have to be established first using Connect-SPOService command.

####################################################################################################
#
#  Author.......: David Shvartsman
#  Date.........: 05/04/2018
#  Description..: Create a Communication Site 
#
#################################################################################################### 
CLS
# Tenant Configuration
$tenantName = "Tenant Name" 
$AdminURI = "https://$($tenantName)-admin.sharepoint.com"
$PersonalURI = "https://$($tenantName)-my.sharepoint.com"
$TenantURI = "https://$($tenantName).sharepoint.com"

#Site Configuration Parameters
$SiteTitle = "Test Communication Site"
$SiteUrl = "CommunicationTestSite" # Note this URL must be available (check with "/_api/GroupSiteManager/GetValidSiteUrlFromAlias")
$SiteTemplate = "6142d2a0-63a5-4ba0-aede-d9fefca2c767" #"Showcase" = "6142d2a0-63a5-4ba0-aede-d9fefca2c767" and "Blank" = "f6cc5403-0d63-442e-96c0-285923709ffc"
$SiteDescription = "Test Communication Site"
$siteClassification = "LBI"
$siteLCID = 1033

$SiteFullURL = "$($TenantURI)/sites/$($SiteUrl)"

# Communication site creation request
$RequestBody = @{
    request = @{__metadata = @{type ="SP.Publishing.CommunicationSiteCreationRequest"}
    AllowFileSharingForGuestUsers = 'false'
    Classification = $siteClassification
    Description = $SiteDescription
    SiteDesignId = $SiteTemplate
    Title = $SiteTitle
    Url = $SiteFullURL
    lcid = $siteLCID
    }
}
$jsonBody = ConvertTo-Json $RequestBody

$contentType = 'application/json;odata=verbose'

# Get a user based context for SharePoint (app credentials not supported for this approach)
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($TenantURI)
$Context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $SecurePWD)
$Context.ExecuteQuery()

# Get url, cookie and forms digest for authentication
$RequestUrl = "$($TenantURI)/_api/sitepages/communicationsite/create"
$AuthenticationCookie = $Context.Credentials.GetAuthenticationCookie($TenantURI, $true)
$FormsDigest = $Context.GetFormDigestDirect()

$WebSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$WebSession.Credentials = $Context.Credentials
$WebSession.Cookies.SetCookies($TenantURI, $AuthenticationCookie)

$Headers = @{ 
    'X-RequestDigest' = $FormsDigest.DigestValue;
    'accept' = 'application/json;odata=verbose';
    'content-type' = 'application/json;odata=verbose' }

# Call REST API to create new site
try {
    $Result = Invoke-RestMethod -Method Post -WebSession $WebSession -Headers $headers -Body $jsonBody -Uri $RequestUrl -UseDefaultCredentials -ContentType $contentType  
    # Site has been created
    Write-Output "New site created at: $($Result.d.Create.SiteUrl), Status: $($Result.d.Create.SiteStatus)"
}
catch {
    Write-Host "Error creating a site"
}
finally {
    $Context.Dispose()
}

Currently SharePoint Admin Portal does not show Communication Sites. To show a list of all Communication Sites using the SharePoint Online Management Shell use the following code:

####################################################################################################
#
#  Author.......: David Shvartsman
#  Date.........: 05/04/2018
#  Description..: List all Communication Site 
#
#################################################################################################### 
Get-SPOSite -Template SITEPAGEPUBLISHING#0 -Limit ALL

To use CSOM or PnP libraries check out How to list all Communication sites in your tenant internet article.

That was another page in the Chronicles of SharePoint Bits, happy scripting!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.