Save SharePoint List as template and create new one




SharePoint list (SPList) has grown up to immense size and started to decrease performance. To solve this, I decided to cut this SPList to pieces – to create archives and save items there by year.

I had to make three operations:

1.       Save SharePoint list as template without data
2.       Create lists from template with custom names
3.       Move items from one list to another

Moving items is a specific tool and there are many ways to move items. I will describe it a bit later. But now here are two script – how to save SPList as template and how to create a new one from template.

Save list as template 

#Save SharePoint list as template 
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) { 
    Add-PSSnapin "Microsoft.SharePoint.PowerShell" 
} 

#Configuration parameters 
$WebURL="https://sharepoint/site/spweb/" 
$ListName="ListName" 
$TemplateName="ListTemplateName" 
$TemplateFileName="ListTemplateFileName" 
$TemplateDescription="List Template Description" 
$SaveData = $False #($True - save data, $False - do not save data ) 


#Get the Web and List objects 
$Web = Get-SPWeb $WebURL 
$List = $Web.Lists[$ListName] 

#Save List as Template 
$List.SaveAsTemplate($TemplateFileName, $TemplateName, $TemplateDescription, $SaveData) 
Write-Host "List Saved as Template!"

 

Create SharePoint list from template 

#Create SharePoint list from template 
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) { 
    Add-PSSnapin "Microsoft.SharePoint.PowerShell" 
} 

#Configuration parameters 

$siteUrl = https://sharepoint/ 
$WebURL= https://sharepoint/site/spweb/ 

$site = Get-SPSite $siteUrl 
$Web = Get-SPWeb $WebURL 
$ListName="List Name" 
$ListURL="listname" 

$listTemplates = $site.GetCustomListTemplates($Web) 
$templateList = $listTemplates['ListTemplateFileName'] 

$Web.Lists.Add($ListURL,  $ListName, $templateList) 

 

 




No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment





MarkiMarta.com. Notes of web-specialist
Since 2009
18+