Skip to content

Instantly share code, notes, and snippets.

@vgrem
Last active August 11, 2017 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vgrem/881add5af0847d5bb4febabec6623084 to your computer and use it in GitHub Desktop.
Save vgrem/881add5af0847d5bb4febabec6623084 to your computer and use it in GitHub Desktop.
Create Office365 Communication Site
#[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.dll")
#[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime.dll")
#[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Online.SharePoint.Client.Tenant.dll")
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\SharePoint Client Components\16.0\Assemblies\Microsoft.Online.SharePoint.Client.Tenant.dll"
function Connect-SPO ([string] $Username, [string]$Password, [string]$TenantUrl) {
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($TenantUrl)
$Context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $(ConvertTo-SecureString -AsPlainText $Password -Force))
$Context.ExecuteQuery()
$AuthenticationCookie = $Context.Credentials.GetAuthenticationCookie($TenantUrl, $true)
$WebSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$WebSession.Credentials = $Context.Credentials
$WebSession.Cookies.SetCookies($TenantUrl, $AuthenticationCookie)
$FormsDigest = $Context.GetFormDigestDirect()
$WebSession.Headers['X-RequestDigest'] = $FormsDigest.DigestValue
$Context.Dispose()
return $WebSession
}
function Invoke-RestSPO ([Microsoft.PowerShell.Commands.WebRequestSession] $Session, [string]$RequestUrl, [string]$Method = "Get", [string]$Payload = $null) {
$Headers = @{
'accept' = 'application/json;odata=verbose'
'content-type' = 'application/json;odata=verbose'
}
if ($Method -eq "Post") {
$Headers['X-RequestDigest'] = $Session.Headers['X-RequestDigest']
$Response = Invoke-RestMethod -Method $Method -WebSession $Session -Headers $Headers -ContentType "application/json;odata=verbose;charset=utf-8" -Body $Payload -Uri $RequestUrl -UseDefaultCredentials
}
else {
$Response = Invoke-RestMethod -Method $Method -WebSession $Session -Headers $Headers -ContentType "application/json;odata=verbose;charset=utf-8" -Uri $RequestUrl -UseDefaultCredentials
}
return $Response
}
function Create-CommunicationSite ([Microsoft.PowerShell.Commands.WebRequestSession] $Session, [string]$TenantUrl, [string]$Title, [string]$SiteUrl, [string]$SiteTemplate) {
$siteTemplateIds = @{
"Topic" = ""
"Showcase" = "6142d2a0-63a5-4ba0-aede-d9fefca2c767"
"Blank" = "f6cc5403-0d63-442e-96c0-285923709ffc"
}
$siteCreationRequest = @{
request = @{
__metadata = @{'type' = 'SP.Publishing.PublishingSiteCreationRequest' };
Title = $Title;
Description = "";
Url = $SiteUrl;
Classification = "";
AllowFileSharingForGuestUsers = $false;
}
}
if($SiteTemplate -ne "Topic"){
$siteCreationRequest.request.SiteDesignId = $siteTemplateIds[$SiteTemplate]
}
$siteCreationRequestJson = $siteCreationRequest | ConvertTo-Json
$requestUrl = "$($TenantUrl)_api/sitepages/publishingsite/create"
$data = Invoke-RestSPO -Session $Session -RequestUrl $requestUrl -Method "Post" -Payload $siteCreationRequestJson
return $data
}
@vgrem
Copy link
Author

vgrem commented Aug 11, 2017

Usage

$TenantUrl = "https://contoso.sharepoint.com/"
$Username = "--username--"
$Password = "---password--"

$session = Connect-SPO -Username $Username -Password $Password -TenantUrl $TenantUrl
$result = Create-CommunicationSite -Session $session -TenantUrl $TenantUrl -SiteUrl "https://contoso.sharepoint.com/sites/mycommsite" -Title "My Communication Site" -SiteTemplate "Topic"
Write-Output "Communication site has been created at: $($result.d.Create.SiteUrl)"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment