kwizcom banner advertisement

How to Upload a List Template in SharePoint 2013 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 2013:
Here is the PowerShell script to upload custom list template to SharePoint site :
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue

#Define Variables
$WebURL = "http://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: http://intranet.crescent.com/_catalogs/lt/Forms/AllItems.aspx
how to upload list template in sharepoint 2013 using powershell
Lets wrap the code inside a function and add some error handling.

Upload List Template to SharePoint using PowerShell
This PowerShell script uploads 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 = "http://intranet.crescent.com"
$TemplateFilePath = "C:\Downloads\projects.stp"

#Call the function to upload list template
Upload-ListTemplate  -WebAppURL $WebAppURL -TemplateFilePath $TemplateFilePath 
How to Upload a List Template in SharePoint 2013 using PowerShell? How to Upload a List Template in SharePoint 2013 using PowerShell? Reviewed by Salaudeen Rajack on January 25, 2014 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.