PowerShell CSOM 備忘録

更新 2020/04/18

この記事の内容
  1. PowerShell CSOM(Client SharePoint Object Model) を使った備忘録
  2. SharePoint Online へのログイン
  3. リスト情報の取得
  4. URL を直接指定
  5. リストタイトルを指定
  6. リスト ID を指定
  7. 列のプロパティを変更
  8. 画面によって列の表示・非表示を切り替える

PowerShell CSOM(Client SharePoint Object Model) を使った備忘録

SharePoint Online へのログイン

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")

# 取得する SharePoint Online の URL
$url = 'https://<your tenant>/<your site>'

# ユーザー名
$user = '<your account>';

# パスワード
$secure = Read-Host -Prompt "Enter the password for ${user}(Office365)" -AsSecureString;

# SharePoint Online 認証情報
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($user, $secure);

# SharePoint Client Context インスタンスを生成
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$ctx.Credentials = $credentials

リスト情報の取得

URL を直接指定

$ctx.Web.GetList('リストまでの絶対パス、又は相対パス')

# 例. $ctx.Web.GetList('https://example.sharepoint.com/learn/Lists/test')
# 例. $ctx.Web.GetList('/learn/Lists/test')

リストタイトルを指定

$ctx.Web.Lists.GetByTitle('リストのタイトル')

# 例. $ctx.Web.Lists.GetByTitle('テストリスト')

リスト ID を指定

$ctx.Web.Lists.GetById('リストの ID')

# 例. $ctx.Web.Lists.GetById('ec173195-aid8-42d6-9ll6-87oie35t0cd3')

列のプロパティを変更

画面によって列の表示・非表示を切り替える

# リストの情報を取得
$list = $ctx.Web.GetList('path')

# フィールドの情報を取得
$fields = $list.Fields

# ロードとサーバーへ問合せ
$ctx.Load($fields)
$ctx.ExecuteQuery()

# 変更する列の取得
$target = $fields.GetByInternalNameOrTitle('column name')

# ロードとサーバー問合せ
$ctx.Load($target)
$ctx.ExecuteQuery()

# アイテムの詳細画面では表示
$target.SetShowInDisplayForm($true)

# アイテムの新規画面では表示
$target.SetShowInNewForm($true)

# アイテムの編集画面では非表示
$target.SetShowInEditForm($false)

# 列情報の更新予約と更新
$target.Update()
$ctx.ExecuteQuery()
ページの先頭へ ↑