Last active
August 11, 2017 12:12
-
-
Save vgrem/881add5af0847d5bb4febabec6623084 to your computer and use it in GitHub Desktop.
Create Office365 Communication Site
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage