简体   繁体   中英

Create a Document Library from a Template using SharePoint Online PowerShell

How do I create a new document library from an existing document library saved as template using SharePoint Online PowerShell?

I was trying the following:

New-PnPList -Title "Test" -Template DocLibTemplate.stp -Url Test

I had no luck on CSOM PowerShell... PnP seemd to be the best option available.

The following PowerShell for your reference.

$ListTemplateInternalName="DocLibTemplate.stp"
$ListName ="PnpDocument"
Connect-PnPOnline -Url https://xx.sharepoint.com/sites/lz -Credential (Get-credential)

$Context = Get-PnPContext
$Web = $Context.Site.RootWeb
$ListTemplates = $Context.Site.GetCustomListTemplates($Web)
$Context.Load($Web)
$Context.Load($ListTemplates)
Invoke-PnPQuery

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

if ($ListTemplate -eq $null)
{
    Throw [System.Exception] "Template not found"
}

$ListCreation = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$ListCreation.Title = $ListName
$ListCreation.ListTemplate = $ListTemplate

$Web.Lists.Add($ListCreation)
Invoke-PnPQuery

Disconnect-PnPOnline

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM