kwizcom banner advertisement

How to Import List Template in SharePoint Online using PowerShell?

Requirement: Import List Templates in SharePoint Online using PowerShell

Why we need list templates in SharePoint Online? Well, If you have a list with set of columns and customization in SharePoint Online, You can reuse it in any other site collections without re-creating the list again from the scratch. A list template contains fields, formulas, formatting, and optionally data as well. List Templates can be downloaded and uploaded to another 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 re-use. Here is how to upload a list template to SharePoint Online.
  • 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?)
  • Click on "Upload" button from the Files tab of the ribbon
    how to import list template in sharepoint online
  • Upload the .stp file that you downloaded from the site collection.
    upload list template sharepoint online powershell
  • Provide a Name to 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. Lets see SharePoint Online PowerShell to upload 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://crescenttech.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 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 Import List Template in SharePoint Online using PowerShell? How to Import List Template in SharePoint Online using PowerShell? Reviewed by Salaudeen Rajack on January 30, 2019 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.