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:

SharePoint Online List Template Gallery URL
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:

  1. 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?)
  2. Click on the “Upload” button from the Files tab of the ribbon how to import list template in sharepoint online
  3. Upload the .stp file that you downloaded from the site collection. upload list template sharepoint online powershell
  4. Provide a Name and Title to the Template and Hit Save.
    import list template to sharepoint online

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 a 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.

upload list template to multiple sites from CSV file

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
}

Last but not least: You must enable custom scripting to enable the “List template” on your site: How to Enable Custom Scripting in SharePoint Online?

Once the list templates are uploaded, you can create new instances of the lists from the template either from the web browser interface or with PowerShell: How to Create a List from Template in SharePoint Online?

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

5 thoughts on “How to Upload List Template to SharePoint Online using PowerShell?

  • I was able to import the .STP file using the PowerShell script for Discussion Board in SharePoint 2016 classic. However, it’s not showing any additional metadata that it should have read from the template file like, Created By (it is showing my name on all the posts), Latest reply by and actual replies. Also, when I add a new discussion it will allow me to do so but users cannot reply to it for see the contents after clicking on it.

    Reply
  • 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

    Reply
  • 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

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *