Create document library from template with PowerShell
-
08-02-2021 - |
Question
I'm attempting to create a document library from a .stp
template.
To my knowledge the script worked and now its not. I get an error when the following is executed:
$ListCreation.ListTemplate = $ListTemplate
Exception setting "ListTemplate": "Cannot convert the "Microsoft.SharePoint.Client.ListTemplate" value of type "Microsoft.SharePoint.Client.ListTemplate" to type "Microsoft.SharePoint.Client.ListTemplate"."
I thought it may be the that I've referenced the wrong dll's .
Have re-installed:
- Microsoft.SharePoint.Client.dll v 16.0.6518.1200
- Microsoft.SharePoint.Client.Runtime.Portable.dll v 16.0.6518.1200
Any ideas on the error ?
Thanks
Solution
You could re-install the SharePoint Online Client Components SDK and SharePoint Online Management Shell.
And there is a demo for your reference to create the document library from custom template by PnP PowerShell.
$username = "username@tenant.onmicrosoft.com"
$password = "password"
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
$Url = "https://tenant.sharepoint.com/sites/tst" # URL of the site
$ListTemplateInternalName = "TestLibTemplate.stp" # Change this with your own list template
$ListName = "NewLibBaseonTemplate2" # Change this with the name of the list to be created
# Create list from template
Connect-PnPOnline -Url $Url -Credentials $cred
$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
Write-Host "Script complete"
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange