Save A List As Template In SharePoint Online Using PowerShell

Introduction

 
In SharePoint, the list templates provide re-usability of columns without recreating it on every single site. For e.g. I have created a custom list which name is Project, in a SharePoint site collection with all relevant columns, Now I want this list structure in other site collections, without recreating the list on every single site.
 

Steps to save a list or library as a template

 
Step-1
 
Go to a SharePoint list, then navigate to list settings present under the list tab on the ribbon and then click on List Settings.
 
Step-2
 
Click on Save list as Template under Permissions and Management group.
 
Save A List As Template In SharePoint Online Using PowerShell
 
Step-3
 
Provide template Filename and Template name. If you want to include the content, then select the check box, Click Ok
 
Save A List As Template In SharePoint Online Using PowerShell
 
Step-4
 
Click OK on the success page.
 
Save A List As Template In SharePoint Online Using PowerShell
 

How to download the list template

 
Step 1
 
Go to site setting, Click on List Templates under web Designers and Gallery.
 
Step 2
 
Click on Upload Document on the Files tab, then browse your file i.e-Demo.stop
 
Save A List As Template In SharePoint Online Using PowerShell
 
You can also save the list template to your site by using PowerShell script,
  1. try {  
  2.     Add - Type - Path "..\Microsoft.SharePoint.Client.dll"  
  3.     Add - Type - Path "..\Microsoft.SharePoint.Client.Runtime.dll"  
  4.     $SiteUrl = Read - Host "Enter the SiteUrl"  
  5.     $UserName = Read - Host "Enter the username"  
  6.     $Password = Read - Host - AsSecureString Password  
  7.     $ctx = New - Object Microsoft.SharePoint.Client.ClientContext($siteUrl)  
  8.     $Credentials = New - Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $Password)  
  9.     $ctx.Credentials = $credentials  
  10.     $Web = $Ctx.Web  
  11.     $site = $Ctx.Site  
  12.     $ctx.Load($web)  
  13.     $ctx.Load($site)  
  14.     $ctx.ExecuteQuery()  
  15.     $ListName = Read - Host "Enter the List Name"  
  16.     $list = $Ctx.Web.lists.GetByTitle($ListName)  
  17.     $FileName = Read - Host "Enter the File Name"  
  18.     $TemplateName = Read - Host "Enter the Template Name"  
  19.     $list.SaveAsTemplate($FileName, $TemplateName, "list template description", $true)  
  20.     $ctx.ExecuteQuery()  
  21.     Write - Host - ForegroundColor Green "Successfully save the list Template."  
  22.     sleep 10  
  23. catch {  
  24.     Write - Host - ForegroundColor Red 'Error '':'  
  25.     $Error[0].ToString();  
  26.     sleep 10  
  27. }  
Follow the below screenshot and enter the values into the parameters accordingly. Then you can get a successful message.
 
Save A List As Template In SharePoint Online Using PowerShell
 

How to upload list template to another site

 
step 1
 
Go to site setting, Click on List Templates under web Designers and Gallery.
 
Step 2
 
Click on Upload Document in the Files tab, Then browse your file i.e-Demo.stp
 
Save A List As Template In SharePoint Online Using PowerShell
 
Save A List As Template In SharePoint Online Using PowerShell 
 
Step 3
 
Save the list template.
 
Save A List As Template In SharePoint Online Using PowerShell
 
Save A List As Template In SharePoint Online Using PowerShell 
 
Step 4
 
Go to Site contents, Click on New then select App.
 
Save A List As Template In SharePoint Online Using PowerShell
 
Step 5
 
Select your template.
 
Save A List As Template In SharePoint Online Using PowerShell
 
Step 6
 
Provide a list Name.
 
Save A List As Template In SharePoint Online Using PowerShell
 
Step 7
 
Now your list added to the site successfully.
 
Save A List As Template In SharePoint Online Using PowerShell
 
You can also add the list template to your site by using PowerShell script,
  1. try {  
  2.     Add - Type - Path "..\Microsoft.SharePoint.Client.dll"  
  3.     Add - Type - Path "..\Microsoft.SharePoint.Client.Runtime.dll"  
  4.     $SiteUrl = Read - Host "Enter the SiteUrl"  
  5.     $UserName = Read - Host "Enter the username"  
  6.     $Password = Read - Host - AsSecureString Password  
  7.     $ctx = New - Object Microsoft.SharePoint.Client.ClientContext($siteUrl)  
  8.     $Credentials = New - Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $Password)  
  9.     $ctx.Credentials = $credentials  
  10.     $Web = $ctx.Web  
  11.     $site = $ctx.Site  
  12.     $ctx.Load($web)  
  13.     $ctx.Load($site)  
  14.     $ctx.ExecuteQuery()  
  15.     $Template = $site.GetCustomListTemplates($Web)  
  16.     $Lists = $ctx.Web.Lists  
  17.     $ctx.Load($Lists)  
  18.     $ctx.Load($Template)  
  19.     $ctx.ExecuteQuery()  
  20.     $TemplateName = Read - Host "Enter the Template Name"  
  21.     $ListName = Read - Host "Enter the List Name"  
  22.     $ListTemplate = $Template | where {  
  23.         $_.Name - eq $TemplateName  
  24.     }  
  25.     $List = $Lists | where {  
  26.         $_.Title - eq $ListName  
  27.     }  
  28.     If($List - eq $Null) {  
  29.         $ListCreation = New - Object Microsoft.SharePoint.Client.ListCreationInformation  
  30.         $ListCreation.Title = $ListName  
  31.         $ListCreation.ListTemplate = $ListTemplate  
  32.         $List = $Lists.Add($ListCreation)  
  33.         $ctx.ExecuteQuery()  
  34.     }  
  35.     Write - Host - ForegroundColor Green "Successfully Add the list."  
  36.     sleep 10  
  37. catch {  
  38.     Write - Host - ForegroundColor Red 'Error '':'  
  39.     $Error[0].ToString();  
  40.     sleep 10  
  41. }  
Follow the below screenshot and enter the values for the parameters accordingly. Then you can get a successful message.
 
Save A List As Template In SharePoint Online Using PowerShell