How to upload a List Template in SharePoint using PowerShell?
Requirement: Upload a custom list template to specific site collections in a web application. While upload a list template using from SharePoint web interface is simple, wanted to automate this process as its repeating for multiple site collections.
PowerShell to upload list template in SharePoint:
Here is the PowerShell script to upload custom list template to SharePoint site :
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
#Define Variables
$WebURL = "https://intranet.crescent.com"
$TemplateFilePath = "C:\Downloads\Projects.stp"
#Get the Web
$web = Get-SPWeb $WebURL
#Get the List template Gallery Folder
$TemplateFolder = $web.GetFolder("List Template Gallery")
#Get List Templates collection
$TemplateFileCollection = $TemplateFolder.Files
#Get the Template file from Local File system
$TemplateFile = Get-ChildItem $TemplateFilePath
#Open the File in Read mode and Add to Templates collection
$TemplateFileCollection.Add("_catalogs/lt/$($TemplateFile.Name)", $TemplateFile.OpenRead(), $true)
Write-Host "Done! List Template has been uploaded!"
You can verify the uploaded template by navigating to List template gallery: https://intranet.crescent.com/_catalogs/lt/Forms/AllItems.aspx
Lets wrap the code inside a function and add some error handling.
Upload List Template to SharePoint using PowerShell
This PowerShell script uploads the list template to all site collections in a given web application.
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
#Function to upload list template in all site collections in a web App
Function Upload-ListTemplate($WebAppURL, $TemplateFilePath)
{
Try {
#Get all site collections of the web app
$SiteCollection = Get-SPWebApplication $WebAppURL | Get-SPSite
#Get the Template file from Local File system
$TemplateFile = Get-ChildItem $TemplateFilePath
#Loop through each site collection
ForEach($Site in $SiteCollection)
{
Write-host "Uploading List Template to Site:"$Site.Url
#Try to Get the List template
$ListTemplate = $Site.GetCustomListTemplates($Site.RootWeb) | where {$_.InternalName -eq $TemplateFile.Name}
#Check if the list template exists
If($ListTemplate -eq $null)
{
#Uplod the List template file
$ListTemplateFile = $Site.RootWeb.GetFolder("_catalogs/lt").Files.Add("_catalogs/lt/$($TemplateFile.Name)", $TemplateFile.OpenRead(), $true)
write-host -f Green "`t List Template Uploaded to Site:"$Site.url
}
Else
{
write-host -f Yellow "`tList Template '$($TemplateFile.Name)' already exists!"
}
}
}
Catch {
write-host -f Red "Error Uploading List Template to Site!" $_.Exception.Message
}
}
#Define Variables
$WebAppURL = "https://intranet.crescent.com"
$TemplateFilePath = "C:\Downloads\projects.stp"
#Call the function to upload list template
Upload-ListTemplate -WebAppURL $WebAppURL -TemplateFilePath $TemplateFilePath
To upload a list template to SharePoint Online, use: Upload list template to SharePoint Online using PowerShell
When I upload the template file using the script, the Created By/ Author names are changing to mine. Is that something that will happen?
Of course! You can update the Created By/Modified By metadata values, if needed! Update “Created By”, “Last Modified” Metadata Fields of a List Item using PowerShell
Hey thanks for the nice script. How would you activate a site template in the solution?