How to Upload List Template to SharePoint Online using PowerShell?
Requirement: Import List Templates in SharePoint Online using PowerShell
Why do we need list templates in SharePoint Online? Well, If you have a list with a set of columns and customization in SharePoint Online, You can reuse it in any other site collection without re-creating the list again from scratch. A list template contains fields, formulas, formatting, and optionally data as well. List Templates can be downloaded and uploaded to other sites. Here is how to use list template SharePoint Online:
List template gallery in SharePoint Online URL: https://yourdomain.sharepoint.com/_catalogs/lt/Forms/AllItems.aspx
How to import a List Template in SharePoint Online?
List templates (.STP) downloaded should be uploaded to any site collection’s list template gallery in order to reuse. Here is how to upload a list template to SharePoint Online:
- Go to Settings >> Site settings page >> Click on “List templates” under Web Designer Galleries (List Template Gallery is Missing in SharePoint Online? Here is the fix: List Template Gallery is not available in SharePoint Online?)
- Click on the “Upload” button from the Files tab of the ribbon
- Upload the .stp file that you downloaded from the site collection.
- Provide a Name to and Title to the Template and Hit Save.
Now, your list template should be available as an APP in the site collection you uploaded the list template. Let’s see SharePoint Online PowerShell to upload the list template.
Upload List Template in SharePoint Online using PowerShell
Here is how to import a list template in SharePoint Online using PowerShell:
#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"
#Function to Upload a list template To SharePoint Online using powershell
Function Upload-SPOListTemplate
{
param
(
[string]$SiteURL = $(throw "Enter the Site URL!"),
[string]$ListTemplateName = $(throw "Enter the List Template Name!"),
[string]$ListTemplateFile = $(throw "Enter the File Name to Upload List Template!")
)
Try {
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the "List Templates" Library
$List= $Ctx.web.Lists.GetByTitle("List Template Gallery")
$ListTemplates = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$Ctx.Load($ListTemplates)
$Ctx.ExecuteQuery()
#Check if the Given List Template already exists
$ListTemplate = $ListTemplates | where { $_["TemplateTitle"] -eq $ListTemplateName }
If($ListTemplate -eq $Null)
{
#Get the file from disk
$FileStream = ([System.IO.FileInfo] (Get-Item $ListTemplateFile)).OpenRead()
#Get File Name from source file path
$TemplateFileName = Split-path $ListTemplateFile -leaf
#Upload the File to SharePoint Library
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $FileStream
$FileCreationInfo.URL = $TemplateFileName
$FileUploaded = $List.RootFolder.Files.Add($FileCreationInfo)
$Ctx.Load($FileUploaded)
$Ctx.ExecuteQuery()
#Set Metadata of the File
$ListItem = $FileUploaded.ListItemAllFields
$Listitem["TemplateTitle"] = $ListTemplateName
$Listitem["FileLeafRef"] = $ListTemplateName
$ListItem.Update()
$Ctx.ExecuteQuery()
#Close file stream
$FileStream.Close()
write-host -f Green "List Template '$ListTemplateFile' Uploaded to $SiteURL"
}
else
{
Write-host -f Yellow "List Template '$ListTemplateName' Already Exists"
}
}
Catch {
write-host -f Red "Error Uploading List Template!" $_.Exception.Message
}
}
#Variables
$SiteURL = "https://Crescent.sharepoint.com/"
$ListTemplateName= "Projects Template V4"
$ListTemplateFile = "C:\Temp\CrescentProject.stp"
#Call the function to Download the list template
Upload-SPOListTemplate -SiteURL $SiteURL -ListTemplateName $ListTemplateName -ListTemplateFile $ListTemplateFile
This uploads the list template to SharePoint Online using PowerShell. To upload a list template to SharePoint Online, use: SharePoint Online PowerShell to Upload a List Template
How to upload List Template to Multiple Sites from a CSV File?
Say, you have a CSV file with a list of site URLs and want to upload the list template to all sites in the CSV file. Well, here is how to deploy list definitions STP to SharePoint Online using PowerShell.
Just replace this script from Line#75 in the above script.
#Read CSV file
$CSVData = Import-CSV "C:\Temp\sites.csv" -Header SiteURL
#Iterate through each row
ForEach($Row in $CSVData)
{
Write-host "Uploading to Site:"$Row.SiteURL
#Call the Function to upload list template to site
Upload-SPOListTemplate -SiteURL $Row.SiteURL -ListTemplateName $ListTemplateName -ListTemplateFile $ListTemplateFile
}
Hi Salaudeen, great article, saved me a lot of time researching. One other question, how do you read from a CSV file with a list of all SPO sites where the template is to be uploaded? some steps will be useful as I am not a programmer. thank you. Sabs
Article updated with the script to read from CSV file.
I can’t find powershell command to save list instance as list template (to stp file) or export to stp file?
i tried this from Microsoft as well, https://bit.ly/2IY8kzV
need your advice
Use this PowerShell to Save List as Template in SharePoint Onine: How to Save List as Template in SharePoint Online using PowerShell?