Save a SharePoint Library as a Site Collection List Template Save a SharePoint Library as a Site Collection List Template
Save-a-SharePoint
Share:

Save a SharePoint Library as a Site Collection List Template

SharePoint has the capability to create list templates for future library creation. This is very useful, but the methods to do this programatically are not obvious. Here’s a script I wrote to loop through a specific set of Site Collections off a managed path, and generate a new list template, after first ensuring any previous list template is deleted. Trying to create a template with the name of an existing template will fail.

 

Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
$envrun="Prod"          # selects environment to run in
$TemplateName="MyTemplate Library"
$DeleteTemplates=$true;
 
if ($envrun -eq "Dev")
{
}
elseif ($envrun -eq "Prod")
{
$siteUrl = "http ://SharePoint/ManagedPath/"
$LoopString = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,09"
$LoopStringArr = $LoopString.Split(“,”)
}
elseif ($envrun -eq "UAT")
{
}
else
{
Write-Host "ENVIRONMENT SETTING NOT VALID: script terminating..."
$siteUrl =  $null;
return;
}
 
foreach ($letter in $LoopStringArr)
{
$SiteName=$siteurl+$letter
 
$rootSite = New-Object Microsoft.SharePoint.SPSite($SiteName)
 
 $site=$rootSite  #skipping traversing all sites for now
  write-host $site.Url
 
  if ($DeleteTemplates)
  {
    $rootWeb = $site.RootWeb;
    $l=$rootWeb.Lists
    $lt=$l["list template gallery"]
    $ltItems= $lt.get_Items()
 
    if ($ltItems.Count -gt 0)
    {   # count down, to avoid a delete causing index shift left
        for ($i=$ltItems.Count-1; $i -ge 0; $i--)
        {
          $TemplateItem=$ltItems.get_Item($i)
          if ($TemplateItem.listitems[0].name -like "MyTemplate*")
          {
          try 
            {
            $itemBeingdeletedName=$TemplateItem.listitems[0].name
            $itemBeingdeletedTitle=$TemplateItem.listitems[0].Title
            $TemplateItem.delete()
            Write-Host "Deleted $($itemBeingDeletedTitle),$($itemBeingDeletedName)"
            }
            catch
            {
            Write-Host "FAILED to Delete $($itemBeingDeletedTitle),$($itemBeingDeletedName)"
            }
          }
        }
      }
    }
 
 
 
  if ($true) #optional location for a filter
#  if ($site.Url -like "$siteurl/personal/plau*")  
  {
   Write-Host -foregroundcolor blue "$($site.id) - $($site.Url) - $($site.contentdatabase.id) - $($site.contentdatabase.name)"  
 
 
   $Mylib=$site.RootWeb.lists["MyTemplate DocLib Template"]
   if ($Mylib -eq $null)
   {$Mylib=$site.RootWeb.lists["AnyTemplate DocLib Template"]}  #alternate name
   try
   {$Mylib.SaveAsTemplate($TemplateName,$TemplateName,$TemplateName,$false)}
   catch
   {Write-Host "You gotta use a new template name each time Dude!" -ForegroundColor darkred}
  #Note: $rootweb.Update() is NOT requried
 
} #if Siteurl passes the filter test
} #loop of site collections

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to talk?

Drop us a line. We are here to answer your questions 24*7.