Thursday, April 25, 2013

Save list as a template and create list from template using PowerShell

In this post we are going to see how we can save list as a template that saves it to the list template gallery and then how we can create a new list / library from the template from the saved template earlier.

Here is a simple script that we can write in PowerShell to do that

Here we are saving shared documents library as a template and then creating a library from the saved template.


[xml]$xmlfile = Get-Content ConfigFile.xml

foreach( $sitecoll in $xmlfile.Configuration.SiteCollection) 

    $site = $sitecoll.name


$spSite= Get-SPSite $site

$web = $spSite.OpenWeb()

Write-Host -foregroundcolor yellow 'Saving list as a template.....'

$list = $web.GetList("/sites/SharePointSite/Shared Documents/")

$list.SaveAsTemplate("SDWITHDOCSET","SD with DOCSET","Shared Doc with Doc Set",1)

Write-Host -foregroundcolor Green 'Saving list as a template successfully'

Write-Host -foregroundcolor yellow 'Creating documents library from the template...'

$listTemplates = $spSite.GetCustomListTemplates($web)

$web.Lists.Add("SDFromTemplateUsingPS", "", $listTemplates["SD with DOCSET"])

Write-Host -foregroundcolor green 'Creating documents library from the template completed successfully.'

I hope this helps.

No comments:




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com