3

Would like to know how to upload a .stp file from my D drive to my list templates gallery in my site collection using PowerShell

2
if ($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions = "ReuseThread"} 
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) 
{
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}


Function UploadListTemplate($WebURL, $TemplateFilePath) 
{ 
    #Get the Web 
    $web = Get-SPWeb $WebURL 

    #Get the List template Gallery Folder 
    $TemplateFolder = $web.GetFolder("List Template Gallery") 

    #Get the Files 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!Template has been uploaded!!" 
} 

#Call the function 
UploadListTemplate "http://sharepoint.crescent.com" "D:\Templates\CustomTaskList.stp"

Reference: How to Upload a List Template using PowerShell

0

Some time back I had similar requirement, to upload .stp onto List gallery and then create a list based on it. I cracked this problem, I hope below function can be useful to you.

function UploadAndCreate-CustomListTemplate()
{
param(
    [Parameter(Mandatory=$true)][string]$rooturl,
    [Parameter(Mandatory=$true)][string]$listCreationUrl,
    [Parameter(Mandatory=$false)][System.Net.NetworkCredential]$credentials,
    [Parameter(Mandatory=$true)][string]$listName,
    [Parameter(Mandatory=$true)][string]$listTemplateFileUrl,
    [Parameter(Mandatory=$true)][string]$listTemplateName

)

    try
    {
        # Adding the PowerShell Snapin
        Add-PSSnapin “Microsoft.SharePoint.PowerShell”

        # Get the SiteURL
        $site = get-spsite($rooturl)

        # Get the root web
        $web = $site.RootWeb

        # Get the list template gallery
        $spLTG = $web.getfolder(“List Template Gallery”)

        # Get the list template gallery Collection
        $spcollection = $spLTG.files

        # Get the custom list template file
        $Templatefile = get-item $listTemplateFileUrl

        # Add the custom list template file to gallery
        $titleHashTbl = @{}
        $titleHashTbl.Add("Title", $listTemplateName)
        $folderListTemplate = "_catalogs/lt/" + $listTemplateName + ".stp"
        $spcollection.Add($folderListTemplate,   $Templatefile.OpenRead(),$titleHashTbl,$true)
        Write-Host “Custom Template Uploaded to List Template Gallery Successfully”

        Write-Host “Creating the List based on the Template”
        # Get the custom list templates
        $CustomlistTemplates = $site.GetCustomListTemplates($web)

        #Create the custom list using template
        $site = Get-SPWeb $listCreationUrl
        $site.Lists.Add($listName, $listName, $CustomlistTemplates[$listTemplateName])
        Write-Host "Based on the template List Created"
    }
    catch
    {
        Write-Host ("Error while creating list. Error -->> " + $_.Exception.Message) -ForegroundColor Red
    }

}

Usage:

$rootUrl = "http://somesite.com"
$url = "http://somesite.com" #Or http://somesite.com/subsite
$credentials = Get-Credential
$listName = "Some List"
$listTemplateFileUrl = "d:\whatever"
$listTemplateName = "List Template Name"


UploadAndCreate-CustomListTemplate $rootUrl $url $credentials $listName $listTemplateFileUrl $listTemplateName

Refer this blog

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.