SharePoint Online の新しいコミュニケーション サイトは REST API で作成できるらしい

PowerShell
スポンサーリンク

PowerShell を使ってサイト作成を自動化や簡略化しようとした時に、SharePoint Online Management Shell(SharePoint Online 管理シェル)を使うことが多いのですが、どうやらこの管理シェルからはモダン UI の新しいコミュニケーション サイトは今のところ作成できないようです。

なんとかならないかなー?と、検索してみたことろ、どうやら REST API では新しいコミュニケーション サイトを作成する操作が提供されているようで、下記のようなリファレンスを見つけました。

Creating SharePoint Communication Site using REST
https://docs.microsoft.com/en-us/sharepoint/dev/apis/communication-site-creation-rest

というわけで、さっそく PowerShell からこの REST API を呼んでみたいと思います。

さっそく PowerShell を書いて試す

ものは試しと、見様見真似で下記のようなスクリプトを書きました。

# NuGet から CSOM パッケージをインストールした場合のパス(例)
Add-Type -Path "C:\Program Files\PackageManagement\NuGet\Packages\Microsoft.SharePointOnline.CSOM.16.1.7115.1200\lib\net45\Microsoft.SharePoint.Client.dll"

# Download Center から SharePoint Online Client Components SDK をダウンロードしてインストールした場合のパス(例)
# https://www.microsoft.com/en-us/download/details.aspx?id=42038
# Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"

$rootUrl = "https://<tenant>.sharepoint.com"
$restEndPoint = $rootUrl + "/_api/sitepages/communicationsite/create"

$siteTitle = "Create From PowerShell!!"
$siteDescription = "from PS from PS"
$siteUrl = $rootUrl + "/sites/fromPowerShell"
$siteLCID = "1041"

# サイトデザイン選択
# Topic($null や "null" を指定しても上手く動きませんでした)
# $siteDesignId = "null"
# Showcase
# $siteDesignId = "6142d2a0-63a5-4ba0-aede-d9fefca2c767"
# Blank
# $siteDesignId = "f6cc5403-0d63-442e-96c0-285923709ffc"

# Context を取得
$credential = Get-Credential
$context = New-Object Microsoft.SharePoint.Client.ClientContext($rootUrl)
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credential.UserName,$credential.Password)
$context.ExecuteQuery()

# Cookie と FormDigest を取得
$cookie = $context.Credentials.GetAuthenticationCookie($rootUrl,$true)
$digest = $context.GetFormDigestDirect()

# Request を作成
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$session.Cookies.SetCookies($rootUrl,$cookie)

$contentType = "application/json;odata=verbose"
$headers = @{
  "Accept" = "application/json;odata=verbose"
  "X-RequestDigest" = $digest.DigestValue
}

$body = @{
  request = @{
    "__metadata" = @{"type" = "SP.Publishing.CommunicationSiteCreationRequest"}
    "AllowFileSharingForGuestUsers" = "false"
    "Title" = $siteTitle
    "Description" = $siteDescription
    # Topic で作成するときは、SiteDesignId を Request に含めない
    # "SiteDesignId" = $siteDesignId
    "Url" = $siteUrl
    "lcid" = $siteLCID
  }
}
$jsonBody = ConvertTo-Json $body

# Request Body の JSON を出力
# Write-Output $jsonBody
# 文字エンコードなどは良しなに行う
$encodeBody = [System.Text.Encoding]::UTF8.GetBytes($jsonBody)

# REST API を呼び出す
$result = Invoke-RestMethod -Method Post -ContentType $contentType -Headers $headers -Uri $restEndPoint -Body $encodeBody -WebSession $session
Write-Output $result.d

それで実行してみると…

あれ?リファレンスには書かれていない「SiteStatus」が返ってきました…、「3」はどういう意味なのでしょうか…?んー、気を取り直して指定した URL にアクセスしてみると…

サイトは作成されており、ちゃんと開けるようです。ただ、リファレンスにもある通り「lcid parameter is not currently supported with this API. You can currently only create English sites.(今は lcid パラメータはサポートしていないし、英語のサイトしか作れないよ)」の影響なのか、英語で表示される部分がチラホラ見えたりします。

さいごに

というわけで、REST API を利用すると、新しいコミュニケーション サイトが PowerShell などから作成できるようですが、まだまだ新しい API ということで動作が良くわからないところが多いですね。

ちなみに、GitHub 上で作成が進められている SharePointPnP.PowerShell Commands では、すでに New-PnPSite として同じようなことができるコマンドレットのリファレンスがありました。

PnP-PowerShell/New-PnPSite.md at master · SharePoint/PnP-PowerShell · GitHub
https://github.com/SharePoint/PnP-PowerShell/blob/master/Documentation/New-PnPSite.md

モダン UI のコミュニケーション サイトは、利用できるパーツが増えたり、機能が改善されたりと、ユーザー側のアップデートが進んできていますので、そろそろこうした管理側の仕組みも確認していきたいですね。

タイトルとURLをコピーしました