kwizcom banner advertisement

SharePoint Online: Create List from Custom Template using PowerShell

Requirement: Create List from Custom List Template using PowerShell in SharePoint Online.

SharePoint Online: How to create list from template?
List templates in SharePoint Online saves a lot of time by reusing any existing list or library, which comprises of all columns and optionally the data from the list. You can create any number of copies from the list template.

Assuming you have an existing list template in the site, Here is how to create a list from a template SharePoint Online
  • Login to your SharePoint Online site >> Click on Settings gear icon and click "Add an app"
  • You can use the pagination to find your list template, or use "Find an App" search box and pick your list template.
    sharepoint online create list from custom template
  • Provide a name to your new list and click on "OK". This creates new list from template in SharePoint Online.
  • You will see a new list created with same list schema, and also data if you have selected "Include Content" when creating the list template originally. Lets see the SharePoint Online PowerShell to create list from template.

SharePoint Online: Create List from Template using PowerShell
Lets use PowerShell in SharePoint Online to create list from custom template.
#Load SharePoint Online 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"

#Config Variables
$SiteURL="https://crescent.sharepoint.com"
$ListTemplateName = "Quick Links Template"
$ListName="Quick Links V2"

#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 All Lists
$Lists = $Ctx.Web.Lists
$Ctx.Load($Lists)
$Ctx.ExecuteQuery()

#Get the Custom list template
$ListTemplates=$Ctx.site.GetCustomListTemplates($Ctx.site.RootWeb)
$Ctx.Load($ListTemplates)
$Ctx.ExecuteQuery()

#Filter Specific List Template
$ListTemplate = $ListTemplates | where { $_.Name -eq $ListTemplateName } 
If($ListTemplate -ne $Null)
{
    #Check if the given List exists
    $List = $Lists | where {$_.Title -eq $ListName}
    If($List -eq $Null)
    {
        #Create new list from custom list template
        $ListCreation = New-Object Microsoft.SharePoint.Client.ListCreationInformation
        $ListCreation.Title = $ListName
        $ListCreation.ListTemplate = $ListTemplate
        $List = $Lists.Add($ListCreation)
        $Ctx.ExecuteQuery()
        Write-host -f Green "List Created from Custom List Template Successfully!"
    }
    else
    {
        Write-host -f Yellow "List '$($ListName)' Already Exists!"
    }
}
else
{
    Write-host -f Yellow "List Template '$($ListTemplateName)' Not Found!"
}
SharePoint Online: Create List from Custom Template using PowerShell SharePoint Online: Create List from Custom Template using PowerShell Reviewed by Salaudeen Rajack on August 14, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.