SharePoint Online: How to Create a List Template using PowerShell?
Requirement: Create a List Template in SharePoint Online.
How to Create a List Template in SharePoint Online?
List templates in SharePoint Online provide re-usability without recreating every single field and other settings in the SharePoint list. E.g., you may create a custom list (say: Projects) in a SharePoint site collection with all relevant columns, and then you may need that list structure in other site collections without recreating the list on every single site. To create a list template in SharePoint Online, follow these steps:
- Go to your SharePoint Online List >> Navigate to Settings >> List settings.
- Click on “Save list as Template” under the Permissions and Management group.
- Provide the file name and template name.
- Choose the appropriate option for “include content” and click OK to create a list template in SharePoint Online. You can use the above steps to save the document library as a template as well in SharePoint Online.
SharePoint Online: Save List as Template using PowerShell
When you need to repeatedly create a list in SharePoint Online with some specific columns and items, you may find it helpful to save the list as a template. You can then easily create new list instances by using the template. Let’s use PowerShell to save a list as a template in SharePoint Online:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Set parameter values
$SiteURL="https://Crescent.sharepoint.com/"
$ListName="Projects"
#Configure Save list as template parameters
$FileName="Projects Template"
$TemplateName="Projects Template"
$Description ="List Template for Projects"
$IncludeData = $False
Try{
#Get Credentials to connect
$Cred= Get-Credential
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Get the List
$List = $Ctx.Web.lists.GetByTitle($ListName)
$List.SaveAsTemplate($FileName, $TemplateName, $Description, $IncludeData)
$Ctx.ExecuteQuery()
Write-Host -f Green "List Saved as Template!"
}
Catch {
write-host -f Red "Error Saving List as template!" $_.Exception.Message
}
This PowerShell script creates a list template in SharePoint Online.
Save list as Template is not visible in SharePoint Online? Here is the fix: SharePoint Online: Save List as Template Missing?
PnP PowerShell to Save List as Template
Here is how to save a list as a template in SharePoint Online using PnP PowerShell:
#Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Documents"
$TemplateFileName = "DocumentsTemplate.stp"
$TemplateName = "DocumentsTemplate"
$TemplateDescription=""
$IncludeData = $False
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-credential)
$Context = Get-PnPContext
#Get the List
$List = Get-PnpList -Identity $ListName
#Save List as template
$List.SaveAsTemplate($TemplateFileName, $TemplateName, $TemplateDescription, $IncludeData)
$Context.ExecuteQuery()
This can be useful if you need to create a new list that is similar to an existing list, or if you want to create a template that you can use for future lists. To delete an existing list template, refer to How to Delete a List Template in SharePoint Online using PowerShell?