2
4

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 5 years have passed since last update.

PowerShell で SharePoint Online に CSOM でアクセスしてみる

Posted at

ブログからの転載

しぇあぽいんとさんと仲良くなるために、まずは Windows では一番慣れている PowerShell を使って SharePoint Online へアクセスしてみたメモ。

CSOM (クライアント側オブジェクト モデル) を使って、リスト一覧を取得してみます。

環境

  • Windows 10 Pro
  • PowerShell 5.1

事前準備

SharePoint Online 用の SDK がインストールされていない時はインストールします。

サンプルコード

ユーザーが miyamiya@example.com、SharePoint Online の URL が https://<tenant>.sharepoint.com/sites/example と仮定してサンプルコード書く。

# SDK の読み込み
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime") | Out-Null

# 取得する SharePoint Online の URL
$url    = 'https://<tenant>.sharepoint.com/sites/example';

# ユーザー名
$user   = 'miyamiya@example.com';

# パスワード
$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 インスタンスを生成
$context = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$context.Credentials = $credentials

# サイトにあるリストを取得
$lists = $context.Web.Lists

# 取得予約
$context.Load($lists)

# リスト取得実行
$context.ExecuteQuery()

# リストの作成日時とタイトルを表示
$lists | %{ "Created: {0}, Title: {1}" -f $_.Created, $_.Title }

考察

一旦 Load で予約して、ExecuteQuery で実際に取得するのが慣れるのに時間がかかりそう。

参考

2
4
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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?