6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【PowerShell】SharePoint クライアント オブジェクト モデル (CSOM) を使用した SharePoint Online の操作

Last updated at Posted at 2021-06-11

PowerShell で SharePoint クライアント オブジェクト モデル (以下、CSOM) を使用して、SharePoint でデータを取得、更新、および管理します。

準備

Module のインストール

PowerShell Gallery からModule をインストールします。

Microsoft.Online.SharePoint.PowerShell

# SharePoint Online Management Shell のインストール、更新
if((Get-Module Microsoft.Online.SharePoint.PowerShell -ListAvailable | measure).Count -eq 0) {
    Install-Module Microsoft.Online.SharePoint.PowerShell -Force
} else {
    Update-Module Microsoft.Online.SharePoint.PowerShell
}

# インストールされているモジュールの確認
Get-Module Microsoft.Online.SharePoint.PowerShell -ListAvailable | select Name, Version

SharePointOnline.CSOM

# SharePointOnline.CSOM のインストール、更新
if((Get-Module SharePointOnline.CSOM -ListAvailable | measure).Count -eq 0) {
    Install-Module SharePointOnline.CSOM -Force
} else {
    Update-Module SharePointOnline.CSOM
}

# 使用できるコマンドの一覧
Get-Command -Module SharePointOnline.CSOM | select Name, Version

トラブルシューティング

インストールしたのにコマンドが実行できない場合

インポートしてみて、コマンドが表示されるか確認してください。

# インポート
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking

# 使用できるコマンドの一覧
Get-Command -Module Microsoft.Online.SharePoint.PowerShell | select Name, Version

Install-Module コマンドで「NuGet プロバイダーが必要...」となる場合

セキュリティ上の脆弱性の観点(2020/04 時点)から PowerShell Gallery のセキュリティプロトコルの既定値が TLS1.2 となり、 それ以前のサポートは廃止されました。

SecurityProtocol プロパティに TLS1.2 を追加してインストールコマンドを再度実行します。

# TLS1.2 通信を許可する
if (([Net.ServicePointManager]::SecurityProtocol -and [Net.SecurityProtocolType]::Tls12) -ne [Net.SecurityProtocolType]::Tls12) {
    [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
}

# NuGet インストール
Install-PackageProvider NuGet -MinimumVersion 2.8.5.201 -Force

操作

SharePoint Web サイトの操作

接続用のコンテキストを生成する

# CSOM ライブラリを読み込む
Load-SPOnlineCSOMAssemblies

# テナント名
$Tenant = 'mako0001mockup'
# サイト名
$SiteName = 'testSiteCol0001'

# クライアントコンテキスト生成
$webFullUrl = "https://${Tenant}.sharepoint.com/sites/$SiteName"
$context = New-Object Microsoft.SharePoint.Client.ClientContext($webFullUrl)

[string] $UserName = 'admin@mako0001mockup.onmicrosoft.com'
[string] $Password = 'Mock0001'

# 資格情報設定
$secureString = ConvertTo-SecureString $Password AsPlainText -Force
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $secureString)

以降の処理で $context 変数を使用します。

Web サイトのプロパティを取得する

# Web サイトのプロパティを取得する
$web = $context.Web
$context.Load($web)
$context.ExecuteQuery()

$web.Title

Web サイトのプロパティに書き込む

# Web サイトのプロパティに書き込む
$web = $context.Web
$web.Title = 'New Title'
$web.Description = 'New Description'
$web.Update()
$context.ExecuteQuery()

SharePoint リストの操作

すべての SharePoint リストを取得する

# すべての SharePoint リストを取得する
$lists = $context.Web.Lists
$context.Load($lists)
$context.ExecuteQuery()

$lists | select Id, Title

SharePoint リストを作成および更新する

# SharePoint リストを作成および更新する
$lists = $context.Web.Lists
$creationInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$creationInfo.Title = 'My List'
$creationInfo.TemplateType = [Microsoft.SharePoint.Client.ListTemplateType]::Announcements
$list = $lists.Add($creationInfo)
$list.Description = 'New Description'

$list.Update()
$context.ExecuteQuery()

SharePoint リストを削除する

# SharePoint リストを削除する
$list = $context.Web.Lists.GetByTitle('My List')
$list.DeleteObject()
$context.ExecuteQuery()

SharePoint リストからアイテムを取得する

# SharePoint リストからアイテムを取得する
$list = $context.Web.Lists.GetByTitle('Announcements')
$query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery(100)
$items = $list.GetItems($query)
$context.Load($items)
$context.ExecuteQuery()

$items | % {
    '{0} : {1}' -f $_['Title'], $_['Body']
}

新しいリスト アイテムを作成する

# 新しいリスト アイテムを作成する
$list = $context.Web.Lists.GetByTitle('Announcements')
$itemCreateInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$listItem = $list.AddItem($itemCreateInfo)
$listItem['Title'] = 'My New Item!'
$listItem['Body'] = 'Hello World!'
$listItem.Update()
$context.Load($listItem)
$context.ExecuteQuery()

参考サイト

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?