気ままなに Exchange Server/SharePoint Server

Exchange サーバー、SharePoint サーバーについて勉強したことを記載していきます。

【SharePoint 2010】 カスタムしたリストテンプレートを PowerShell で作成

[目的]
作成したリストテンプレートをPowerShellのコマンドで実行したい。

[調査]
まずはリストテンプレートギャラリーから取得できるか試してみる。

$web = Get-SPWeb "<サイト>"
$list = $web.Lists["リスト テンプレート ギャラリー"]
$listtemplate = $list.Items

上記でとれたようにみえるが、$listtemplate[<テンプレート>] で取得できない。。。
ちゃんと、msdn見ながらじゃないとだめだ。
下記のような記述がある。

https://msdn.microsoft.com/ja-jp/library/microsoft.sharepoint.spsite.getcustomlisttemplates(v=office.12).aspx

つまり、以下でいける。

$web = Get-SPWeb "<サイト>"
$rootweb = $web.Site.RootWeb
$listtemplates = $web.Site.GetCustomListTemplates($rootweb)
$listtemplates["<リストテンプレート名>"]

よし、とれた。
あとは、add で作成するだけ。

[作成]
$web = Get-SPWeb "<サイト>"
$rootweb = $web.Site.RootWeb
$listtemplates = $web.Site.GetCustomListTemplates($rootweb)
$web.Lists.Add("テスト", "テストです", $listtemplates["<リストテンプレート名>"])

以上で完成。