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

Step-3

Provide template File name and Template name. If you want to include the content, then select the check box, Click Ok

Step-4

Click OK on the success page.

How to download the list template –

Step1- Go to site setting, Click on List Templates under web Designers and Gallery.

Step2- Select your List Template and Click to download a copy in the Files tab.

You can also save the list template to your site by using powershell script-

 try{

    Add-Type -Path “..\Microsoft.SharePoint.Client.dll”

    Add-Type -Path “..\Microsoft.SharePoint.Client.Runtime.dll”

    $SiteUrl = Read-Host “Enter the SiteUrl”

    $UserName=Read-Host “Enter the username”

    $Password =Read-Host -AsSecureString Password

    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)

    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$Password)

    $ctx.Credentials = $credentials

    $Web=$Ctx.Web

    $site=$Ctx.Site

    $ctx.Load($web)

    $ctx.Load($site)

    $ctx.ExecuteQuery()

    $ListName=Read-Host “Enter the List Name”

    $list = $Ctx.Web.lists.GetByTitle($ListName)

    $FileName=Read-Host “Enter the File Name”

    $TemplateName=Read-Host “Enter the Template Name”

    $list.SaveAsTemplate($FileName, $TemplateName, “list template description”, $true)

    $ctx.ExecuteQuery()

    Write-Host -ForegroundColor Green “Successfully save the list Template.”

     sleep 10

 }

    catch

    {

        Write-Host -ForegroundColor Red ‘Error ‘,’:’$Error[0].ToString();

        sleep 10

    }

Follow below screenshot and enter the values to the parameters accordingly. Then you can get a successful message.

How to upload list template to another site

Step1- Go to site setting, Click on List Templates under web Designers and Gallery.

Step2- Click on Upload Document on the Files tab then browses your file i.e. Demo. Stop

Step3- Save the list template.

Step4- Go to Site contents, Click on New then select App.

Step5- Select your template.

Step6- Provide a list Name.

Step7- Now your list added to the site successfully.

You can also add the list template to your site by using PowerShell script-

 try{

    Add-Type -Path “..\Microsoft.SharePoint.Client.dll”

    Add-Type -Path “..\Microsoft.SharePoint.Client.Runtime.dll”

    $SiteUrl = Read-Host “Enter the SiteUrl”

    $UserName=Read-Host “Enter the username”

    $Password =Read-Host -AsSecureString Password

    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)

    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$Password)

    $ctx.Credentials = $credentials

    $Web=$ctx.Web

    $site=$ctx.Site

    $ctx.Load($web)

    $ctx.Load($site)

    $ctx.ExecuteQuery()

    $Template =$site.GetCustomListTemplates($Web)

    $Lists = $ctx.Web.Lists

    $ctx.Load($Lists)

    $ctx.Load($Template)

    $ctx.ExecuteQuery()

    $TemplateName=Read-Host “Enter the Template Name”

    $ListName=Read-Host “Enter the List Name”

    $ListTemplate = $Template | where { $_.Name -eq $TemplateName }

    $List = $Lists | where {$_.Title -eq $ListName}

    If($List -eq $Null)

    {

        $ListCreation = New-Object Microsoft.SharePoint.Client.ListCreationInformation

        $ListCreation.Title = $ListName

        $ListCreation.ListTemplate = $ListTemplate

        $List = $Lists.Add($ListCreation)

        $ctx.ExecuteQuery()

    }

    Write-Host -ForegroundColor Green “Successfully Add the list.”

     sleep 10

    }

    catch

    {

        Write-Host -ForegroundColor Red ‘Error ‘,’:’$Error[0].ToString();

        sleep 10

    }

Follow below screenshot and enter the values to the parameters accordingly. Then you can get a successful message.

Key Words:

-Create and save a list as template in SharePoint online using PowerShell
-Create and add the template in SharePoint site using pnp PowerShell.
-How to create and add list template in SharePoint online using PowerShell?
-Create list template and add the list template to another site using PowerShell.

Leave a Reply

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