LoginSignup
1
3

More than 5 years have passed since last update.

ドキュメントライブラリのリストテンプレートをコピーしてドキュメントライブラリを生成するPowerShell

Last updated at Posted at 2018-10-21

リストテンプレートをコピーしてドキュメントライブラリを生成するPowerShell

コンテンツを含む場合は、コンテンツ(ファイル、列の情報)もコピーされますが、件数が多いと処理時間が遅いのである程度を分割して平行で実施した方が良いです。

#モジュールのロード
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")  

$siteURL = "https://xxx.sharepoint.com/sites/xxxxx"
$ListTemplateInternalName = "○○○○○○.stp" #コピー元のテンプレート名
$userName = "user@xxxxx.onmicrosoft.com"
$ListName = "test" #コピー後のドキュメントライブラリ名

#認証
$password = Read-Host "Please enter the password for $($userName)" -AsSecureString 
$SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $password)

$context = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
$context.credentials = $SPOCredentials 

$web = $context.Web
$site = $context.Site
$ListTemplates = $site.GetCustomListTemplates($web)
$context.Load($web)
$context.ExecuteQuery()
$context.Load($ListTemplates)
$context.ExecuteQuery()  

$ListTemplate = $ListTemplates | where-Object { $_.InternalName -eq $ListTemplateInternalName }

#必要なドキュメントライブラリ分だけ繰り返す
for($i = 1; $i -le 100; $i++){
      $ListCreation = New-Object Microsoft.SharePoint.Client.ListCreationInformation
      $ListCreation.Title = $ListName + $i
      $ListCreation.ListTemplate = $ListTemplate

      $newList = $web.Lists.Add($ListCreation)

      $context.Load($newList)
      $context.ExecuteQuery()
}

$context.Dispose()

参考

Create a Document Library from a Template using SharePoint Online PowerShell
Create new document library in SharePoint Online with PowerShell
Create a new list from custom template with CSOM in SharePoint Online

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