SharePoint Online: How to Create a List Template using PowerShell?

Requirement: Create List Template in SharePoint Online

How to Create List Template in SharePoint Online?

List templates in SharePoint Online provide re-usability without having to recreate 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 List settings
  • Click on “Save list as Template” under the Permissions and Management group.
    create list template in sharepoint online
  • Provide the file name and template name
    sharepoint online save list as template powershell
  • 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

Let’s use PowerShell to save list as 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://crescenttech.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.

SharePoint Online Save List as Template URL – https://YourDomain.SharePoint.com/_layouts/15/savetmpl.aspx?List={LIST-GUID}

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 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()

Salaudeen Rajack

Information Technology Professional with Two decades of SharePoint Experience.

Leave a Reply