Sharepoint online client components sdk 日本語

SharepointのカスタムリストをPowershellでUpdateする方法

  • Sharepoint操作に必要な準備をする
    • CSOMの入手
      • SharePoint Online
      • SharePoint Server 2016
      • SharePoint Server 2013
      • SharePoint Server 2010
    • リスト操作の準備
      • CSOMモジュールの読み込み
      • サイトコレクションの読み込み
      • 認証情報の作成
      • Contextの作成
      • リストの名前を指定してリストを読み込む
      • リストのアイテムを読み込む
    • リストのアイテムに対して更新をする
      • テキスト項目を更新する
      • URL項目を更新する
      • ユーザー項目を更新する
      • 更新&片付け
    • サンプルソース

以前ドキュメントライブラリの操作で使用したクライアント サイド オブジェクト モデル (以下、CSOM) のリモート実行モジュールを使います。

ドキュメントライブラリについての操作は下記をご覧ください

OneDrive for Businessの操作を自動化する【Powershell】

CSOMの入手

使用しているSharepointサーバに応じたCSOMモジュールを入手します。

SharePoint Online

Nuget 版
タイトル : Microsoft.SharePointOnline.CSOM
URL : https://www.nuget.org/packages/Microsoft.SharePointOnline.CSOM/

Microsoft Download 版
タイトル : SharePoint Online Client Components SDK
URL : https://www.microsoft.com/en-us/download/details.aspx?id=42038

SharePoint Server 2016

タイトル : SharePoint Server 2016 Client Components SDK
URL : https://www.microsoft.com/en-us/download/details.aspx?id=51679

SharePoint Server 2013

タイトル : SharePoint Server 2013 Client Components SDK
URL : https://www.microsoft.com/en-us/download/details.aspx?id=35585

SharePoint Server 2010

タイトル : SharePoint Foundation 2010 Client Object Model Redistributable
URL : http://www.microsoft.com/download/en/details.aspx?id=21786

リスト操作の準備

CSOMモジュールの読み込み

まずは、CSOMモジュールを読み込みます。結果が返ってくるので|Out-Nullで非表示にしてますが、表示しても一向に構いません。

# Read CSOM

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")|Out-Null

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime") |Out-Null

サイトコレクションの読み込み

まずURLを指定します。OnlineにしろOn-Premiseにしろ、下記のいずれかで指定できると思います。

# SiteCollection URL

$SiteCollectionUrl="https://tenant-name.sharepoint.com"

# $SiteCollectionUrl = "https://tenant-name.sharepoint.com/sites/subsite"

# $SiteCollectionUrl = "http://sharepoint.contoso.local/sites/subusite/"

認証情報の作成

Sharepoint Onlineの場合は認証情報を作成する必要があります。

# Account

$Account=""

$SecurePassword=Read-Host-Prompt"Enter Your Password."-AsSecureString

# Create Credential

$Credential= New-ObjectMicrosoft.SharePoint.Client.SharePointOnlineCredentials($Account,$SecurePassword)

On-Premiseのサーバの場合には、統合認証が使えるので下記でもいけるはず。

# if you use on-premise server,you can use below

$Credential=[System.Net.CredentialCache]::DefaultNetworkCredentials

Contextの作成

Contextを作って、認証情報を付与します。

# Create Context

$Context=New-ObjectMicrosoft.SharePoint.Client.ClientContext($SiteCollectionUrl)

$Context.Credentials= $credential

リストの名前を指定してリストを読み込む

$ListName="ListName"#you can use 2-byte code

# Create List object

$List=$Context.Web.Lists.GetByTitle($ListName)

$Context.Load($List)

$Context.ExecuteQuery()

サンプルでは”ListName”としていますが、2バイトコード指定ができるので日本語でもOKです。

リストの情報を表示してみましょう。

Write-Host"list Title : "$List.Title

Write-Host"list BaseType : "$List.BaseType

Write-Host"list Created : "$List.Created

リストのアイテムを読み込む

CamlQueryを準備してアイテムを読み込みます。xmlが無指定で良ければ全件読み込みます。

$Query=New-ObjectMicrosoft.SharePoint.Client.CamlQuery

$Query.ViewXml=""

$ListItems= $List.getItems($Query)

$Context.Load($ListItems)

$Context.ExecuteQuery()

これで$ListItemsにリストアイテムの配列が格納されます。
何件確認されているか見てみましょう。

これで更新対象のリストが準備できました。

リストのアイテムに対して更新をする

$ListItemsはリストアイテムの配列なので、個々のアイテムを取り出して操作をする必要があります。
$ListItems.getById(1)で取り出せるはずなんですが、筆者の環境ではうまく行かなかったので(T_T)、
foreach文を使うことにします。
まずは試しに内容を表示させてみましょう。

foreach($item in$ListItems)

{

    Write-Host  $item.Id.ToString()`t $item["Title"]`t$item["User"].LookupValue$item["Mail"].LookupValue`t $item["URL"].Description`t$item["URL"].URL

}

ユーザー毎の名前(User)、Mail、個人別サイト(URL)が格納されたリストを想定します。
$item[“Title”]はitemメソッドを使って、$item.item(‘Title’)と表記することも出来ます。

テキスト項目を更新する

テキストの項目であれば、代入するだけで更新できます。

    #Text Field

    $item["Title"]="Test"

    $item.Update()

このUpdateメソッドは手元のオブジェクトを更新するだけなので、サーバに反映するには$Context.ExecuteQuery()メソッドを実行する必要があります。複数行をまとめてExecuteQueryするとコケることがあるので、1アイテムごとにやった方が良さそうです。

選択式の項目もテキスト項目と同じ要領で更新できます。

URL項目を更新する

[ハイパーリンクまたは画像]列はFieldUrlValue型に格納されています。新しいオブジェクト作って、プロパティを設定、FieldUrlValue型にCastしてから格納してすると更新できます。

    #URLValue

    $Mail=$item["Mail"].LookupValue

    $CN= $Mail.Split("@")[0]

    $Url="http://mysite.sharepoint.contoso.local/personal/"+$CN

    $Description =$CN

    $field=New-ObjectMicrosoft.SharePoint.Client.FieldUrlValue

    $field.Description=$Description

    $field.URL=$Url

    if($null-eq$item["URL"]){

    $item["URL"] =[Microsoft.SharePoint.Client.FieldUrlValue]$field

    }

ユーザー項目を更新する

[ユーザーまたはグループ]列はFieldUserValue型に格納されています。Idで管理されていますが、URLと同じ様にFieldUserValue型にCastしてから格納します。

    #UserValue

    $EditorValue=New-ObjectMicrosoft.SharePoint.Client.FieldUserValue

    $EditorValue.LookupId =$item["User"].LookupId

    $EditorValueCollection=[Microsoft.SharePoint.Client.FieldUserValue[]]$EditorValue

    $item["Editor"] =$EditorValueCollection

更新&片付け

一通り更新が終わったらExecuteQueryしてDisposeしてオブジェクトを開放しましょう。

$Context.ExecuteQuery()

# Dispose Context

$Context.Dispose()

サンプルソース