Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 8583

How to create a library from a saved template

$
0
0

Salvete!  I just joined this forum so I could post here!  I have been two days trying to get this to work, and really need some help.

First, I created a forms library, customized it, and saved it as a template.  I can see it in the List Templates Gallery.

I have an new subsite.

I want to write a powershell script that will create a new forms library in my subsite based on my custom library template.

Here is what I have come up with so far:

$siteUrl = "http://mysite"

Function DeployTemplate($whatsubsite, $whattemplate, $whatname, $whattitle, $whatdescription){
    Write-Host "<><><><><><>Configuring $siteurl" $_ -ForegroundColor blue
   
    $libtitle = $whattitle
    $liburl = "$siteUrl/$whatname"
    $libdescription = $whatdescription
    $libtemplate = $whattemplate
   
    try{
        # see if the site already exists
        $currentWeb = Get-SPWeb -Identity $SiteUrl #-ErrorAction SilentlyContinue   
        if ($currentWeb -ne $null) {  #it will be null if it doesn't exist
           
            #New-SPList -Web $currentWeb -ListTitle $thistitle -ListUrl $thisurl -Description $thisdescription -Template $thistemplate
           
            #get our custom template from the list-templates gallery
            $Site = Get-SPSite $siteUrl
            $Web = $Site.RootWeb
            $listTemplates = $Site.GetCustomListTemplates($Web)
            $theTemplate = $listTemplates | Where-Object {$_.InternalName -eq $libtemplate}           
                       
            if($theTemplate){                       
                #Now create the new library from our custom template
                Start-SPAssignment -Global
                $currentWeb.Lists.Add($liburl,$libdescription,$theTemplate)
                $thislist = $currentWeb.Lists[$liburl]
                $thislist.Title = $libtitle
                $thislist.Update()
                Stop-SPAssignment -Global           
                $currentWeb.update()       
            }else{
                Write-Host "  Sorry, the template could not be found: "$libtemplate $_ -ForegroundColor red
            }
           
            $currentWeb.dispose()

        }else{
            Write-Host "  There is no site for "$siteUrl $_ -ForegroundColor cyan
        }
    }catch{
        Write-Host "  caught a fish:" $_ -ForegroundColor red
    } #end try
}#end DeployTemplate

 

It can't seem to locate the custom template.

 


Viewing all articles
Browse latest Browse all 8583

Trending Articles