Save list as template in SharePoint Online using PowerShell

In this tutorial, we will discuss how to save list as a template in SharePoint using PowerShell. It will work in SharePoint 2013/2016 as well as SharePoint 2019. Also, we will see how to save list as template in SharePoint Online using PowerShell and using PnP PowerShell.

Save list as template SharePoint using PowerShell

The code I have used in SharePoint 2016 site, but will work in SharePoint 2013 as well as in SharePoint 2019 as well.

You can run the PowerShell script in PowerShell ISE.

$templateFileName="Product Template File Name"
$templateName="Product Template"
$templateDesc="Use this template to create product list"
$includeContent=$true

$web = Get-SPWeb "http://mypc/sites/MySP2016SiteCollection/"
$List = $web.Lists["Products"]
$List.SaveAsTemplate($templateFileName, $templateName, $templateDesc, $includeContent)

Here, you can provide the list template name, template file name, list template description.

Also, if you want to include the content, then you can make it to true.

Once you run the script, it will create the list template and stored in the SharePoint site list template gallery.

Create a list from SharePoint list template using PowerShell

Now, we will see how to create a list by using the SharePoint list template which we have already created before.

$listname="New List Name"
$web = Get-SPWeb "http://mypc/sites/MySP2016SiteCollection/"
$listTemplates = $site.GetCustomListTemplates($web)
$web.Lists.Add($listname, $listTemplates["Product Template"])

This will create a new list name as “New List Name” in the SharePoint 2013/2016/2019 site.

Create a list by using an existing list in SharePoint Online

If you are using a modern SharePoint Online site, then there is an option we can create a list from an existing list. I have written a detailed blog post, how to create a list from the existing list in SharePoint Online.

Save a list as template in SharePoint Online

You can also use the browser to save list as template in classic SharePoint Online site.

Open the SharePoint list, go to the list settings.

From the list settings page, click on “Save list as template“.

save list as template sharepoint
stp file sharepoint

Then it will ask you to provide the File name, Template name, Template description like below:

Also, if you want to include the list content, then click on “Include Content” like below:

Save list as template in SharePoint Online
stp file sharepoint

Once you click on OK, the list template will be saved in the list template gallery in SharePoint Online or SharePoint 2013/2016/2019.

Save list as template in SharePoint 2013
sharepoint stp file

You can see the list template in the list template gallery in the SharePoint site.

Save list as template in SharePoint 2016
sharepoint stp file

This is how we can save list as template in SharePoint Online or SharePoint 2013/2016/2019.

Save list as template in SharePoint Online PowerShell

Now, we will see how to save list as template in SharePoint Online using PowerShell. Here, we will use CSOM with PowerShell in SharePoint Online.

Here, I have used the below client side dlls.

  • Microsoft.SharePoint.Client.dll
  • Microsoft.SharePoint.Client.Runtime.dll

If you are using Windows 10 laptop, then you can use the SharePoint Online client side component SDK.

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"
$siteUrl = "https://tsinfo.sharepoint.com/sites/TSInfoClassic/"
$username = "bijay@tsinfo.onmicrosoft.com"
$password=ConvertTo-SecureString "***********" -AsPlainText -Force
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
$ctx.Credentials = $credentials
$list = $Ctx.Web.lists.GetByTitle("Products")
$list.SaveAsTemplate("Product Template File Name", "Product Template Name", "This is the product list template description", $true)
$ctx.ExecuteQuery()

Once you execute the above PowerShell script, the SharePoint Online list template like below:

Save list as template in SharePoint 2019
sharepoint list template gallery

Save list as template SharePoint Online using PnP PowerShell

PnP PowerShell is very easy to work with SharePoint Online.

Now, we will see how to save list as template in SharePoint Online using PnP PowerShell.

Connect-PnPOnline -Url "https://tsinfo.sharepoint.com/sites/TSInfoClassic/" -Credentials (Get-credential)
$context  = Get-PnPContext
$list = Get-PnpList -Identity "Products"
$list.SaveAsTemplate("PnP Product List Template Filename", "PnP Product List Template", "This list template is created using PnP", $true)
$context.ExecuteQuery()
sharepoint online save list as template
sharepoint .stp file

Once you execute the code, it will ask you to enter the credentials and then it will Save the list as template in SharePoint.

save list as template sharepoint online pnp powershell
stp file sharepoint

This is how to save list as template SharePoint Online using PnP PowerShell.

How to extract and make SharePoint stp file

We can extract a .stp file and then how we can make a .stp file from the extracted files in SharePoint. We have created a custom list template which usually saves as a .stp file. After saving this .stp file we wanted to modify something in the manifest.xml file

First, you can extract the .stp file. Here I have used 7-Zip to extract it like below:

extract stp file sharepoint
extract stp file sharepoint

Once it is successfully extracted, you can see the manifest.xml file.

Then you can modify the file using Notepad or Notepad++ (any editor you can use). Once the modification over we can run the below command to create the .stp file again.

Here I have kept the manifest.xml file in the d drive and running the below command.

makecab.exe manifest.xml WorkflowMailTemplates.stp

how to edit a list template in sharepoint 2013
Edit a list template in SharePoint 2013

This is how to extract .stp file and make .stp file in SharePoint Online and how to change in manifest.xml file in SharePoint.

You may like:

In this SharePoint tutorial, we learned how to save list as template in SharePoint Online using PowerShell and PnP PowerShell.

Also, we will see how to save a list as template in SharePoint 2013/2016 or SharePoint 2019, or SharePoint Online.

>