LoginSignup
3
3

More than 5 years have passed since last update.

.NET Core で SharePointOnlineに接続する

Posted at

.NET Core でCSOMを使おうとするとハマる点があったので書き残しておく。
(ちなみに初投稿。緊張する...)

前提

Visual Studio 2017 を使用中。
.NET Core のコンソールアプリケーションを作成すること。
SharePointOnlineに接続できる環境であること。

DLLが違う

.NET Framework だったら単純にNuGetパッケージでMicrosoft.SharePointOnline.CSOMをダウンロードして参照すればいいものを、.NET Core はそこからDLL抜き出して別途参照させないといけない。
今回必要だったDLLは以下の3つ。

Microsoft.SharePoint.Client.Portable.dll
Microsoft.SharePoint.Client.Runtime.Portable.dll
Microsoft.SharePoint.Client.Runtime.Windows.dll

DLLはNuGetのギャラリーから取得したものを使用した。
上2つは .NET Core 用のフォルダ?「netcore45」から。

SharePoint.Client.dll や
SharePoint.Client.Runtime.dll
ではなく
SharePoint.Client.Portable.dll
SharePoint.Client.Runtime.Portable.dll
であることが重要。

最後の一つ
SharePoint.Client.Runtime.Windows.dll
は「net45」から取得した。
キャプチャ.PNG

非同期処理に対応する

そしてコードでの相違点は ExecuteQuery にある。
.NET Framework だったら単純にこう↓

clientcontext.ExecuteQuery()

.NET Core ではどうもここが非同期処理になるらしく、Asyncになる。

clientcontext.ExecuteQueryAsync()

非同期処理になるので、直前にLoadしたオブジェクトの取得まで待機しないとオブジェクトの操作ができない。

await clientcontext.ExecuteQueryAsync()

これをしないと、プロパティ値が取得できなかったり(そもそもエラーになる)、このあとに値を追加しても全く反映されなかったりする。

3
3
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
3
3