0

I need to create a Document Library that is based on a custom template (.stp) that is already saved in the Site Collection level (..._catalogs/lt/Forms/AllItems.aspx) in the Template Gallery. The code I have below will create a custom document library based on the template, but this only works for the Site Collection (aka Root Web).

I need this to go into the sub site and can't figure out how to make it happen.

Note: This is using the latest PnP.PowerShell library and not the older SharePointPnPPowerShellOnline Library. This is a similar question to: Create document library from template with PowerShell, but even though the commands are similar to the older library (or the same), we have to stick with PnP.Powershell.

#Params
$UserName = "[email protected]"
$Password = "12345"
$SiteURL= "https://test.sharepoint.com/sites/00005"
$SubWebRelativeUrl = "/MySubSite" 
$ListTemplateInternalName = "CustomTemplate.stp"
$NewListName = "Test Custom Library 101"   

#Connection parameters
$SecurePassword = $AdminPassword | ConvertTo-SecureString -AsPlainText -Force
$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist 
$AdminName, $SecurePassword

#Connect to SharePoint Online
write-host "Connecting to $($SiteURL)$($SubWebRelativeUrl)..."  -ForegroundColor Magenta 
Connect-PnPOnline -Url "$($SiteURL)$($SubWebRelativeUrl)" -Credential $Credentials

#Checking for Template
write-host "Checking for the specified template..."  -ForegroundColor Cyan
$Context = Get-PnPContext
$Site = $Context.Site
$Web = $Context.Site.RootWeb
$ListTemplates = $Context.Site.GetCustomListTemplates($Web)
$Context.Load($Site)
$Context.Load($Web)
$Context.Load($ListTemplates)
Invoke-PnPQuery

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

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

#Create new list  
write-host "Creating new library or list..."  -ForegroundColor Cyan      
$ListCreation = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$ListCreation.Title = $NewListName
$ListCreation.Description = "$($NewListName) DMS Custom Library"
$ListCreation.ListTemplate = $ListTemplate
$ListCreation.Url = "$($SiteURL)$($SubWebRelativeUrl)"
$ListCreation.QuickLaunchOption = 1
$Web.Lists.Add($ListCreation)
Invoke-PnPQuery
write-host "List $($NewListName) created successfully!" -ForegroundColor Green

#Disconnect from SharePoint Online
write-host "Disconnecting from $($SiteURL)$($SubWebRelativeUrl)..."  -ForegroundColor Magenta 
Disconnect-PnPOnline

1 Answer 1

0

The issue of "why" the List / Document Library would only be created in the Root of the website is due to this one line:

$Web = $Context.Site.RootWeb

This causes the $Web variable to be loaded up with the Root Website of the Site Collection regardless of what URL you use in the Connect-PnPOnline command (assuming you put in like /sites/SiteCollectionName/Subsite2/Subsite2).

The correct call you need to do for the $Web variable is below:

$Web = $Context.Site.OpenWeb($SubWebRelativeUrl)

The updated code with the correction is below. I hope this helps others as the original example structure from my question is copied and pasted everywhere on the Internet, but fails to show how to create Lists and Document Libraries off of a custom template (STP file).

#Params
$UserName = "[email protected]"
$Password = "12345"
$SiteURL= "https://test.sharepoint.com/sites/00005"
$SubWebRelativeUrl = "MySubSite" 
$ListTemplateInternalName = "CustomTemplate.stp"
$NewListName = "Test Custom Library 101"   

#Connection parameters
$SecurePassword = $AdminPassword | ConvertTo-SecureString -AsPlainText -Force
$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist 
$AdminName, $SecurePassword

#Connect to SharePoint Online
write-host "Connecting to $($SiteURL)..."  -ForegroundColor Magenta 
Connect-PnPOnline -Url "$($SiteURL)" -Credential $Credentials

#Checking for Template
write-host "Checking for the specified template..."  -ForegroundColor Cyan
$Context = Get-PnPContext
$Site = $Context.Site
$Web = $Context.Site.OpenWeb($SubWebRelativeUrl)
$ListTemplates = $Context.Site.GetCustomListTemplates($Web)
$Context.Load($Site)
$Context.Load($Web)
$Context.Load($ListTemplates)
Invoke-PnPQuery

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

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

#Create new list  
write-host "Creating new library or list..."  -ForegroundColor Cyan      
$ListCreation = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$ListCreation.Title = $NewListName
$ListCreation.Description = "$($NewListName) DMS Custom Library"
$ListCreation.ListTemplate = $ListTemplate
$ListCreation.Url = "$($SiteURL)/$($SubWebRelativeUrl)"
$ListCreation.QuickLaunchOption = 1
$Web.Lists.Add($ListCreation)
Invoke-PnPQuery
write-host "List $($NewListName) created successfully!" -ForegroundColor Green

#Disconnect from SharePoint Online
write-host "Disconnecting from $($SiteURL)$($SubWebRelativeUrl)..."  -    ForegroundColor Magenta 
Disconnect-PnPOnline

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.