Create SharePoint 2013 List using out of box or custom template using PowerShell

Recently We had some requirement to create list in 30 sites and subsites in SharePoint 2013. We created the list and saved the list as a template. Now creating the same list in 30 sites and subsites is a big pain. So thought of writing PowerShell script which will create the list using out of the box template or custom list template. Here we will see how we can create list by using custom list template in SharePoint 2013. The PowerShell script, you can write, test and debug using Windows PowerShell ISE or by using Visual Studio code.

We need to create CSV file with below columns as input variables.
SiteURL
TemplateName
ListName
CustomTemplate – If its custom template we have to pass a parameter as Yes or if you are using OOTB template pass the parameter value as No.

So the CSV will look like below image.

Create SharePoint 2013 List using out of box or custom template using PowerShell
Create SharePoint 2013 List using out of box or custom template using PowerShell



PowerShell Script-

$0 = $MyInvocation.MyCommand.Definition
$dp0 = [System.IO.Path]::GetDirectoryName($0)
$path = $(“$dp0\CreateListsFromTemplates.csv”)

$csv = Import-csv -path $path

foreach($line in $csv)
{
$strWebURL = $line.SiteURL
$customListTemplate = $line.CustomTemplate
if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
Add-PSSnapin Microsoft.SharePoint.PowerShell
}

[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”) | Out-null

$web = Get-SPWeb $strWebURL
$site = $web.Site

$rootweb = $site.OpenWeb()
if($customListTemplate.ToLower() -eq “no”)
{
write-host “List Template: ” $line.TemplateName
$web.Lists.Add($line.ListName, “”, $line.TemplateName)
Write-Host $line.ListName ” list created Successfully…..”
}
else
{
$listTemplates = $site.GetCustomListTemplates($rootweb)
$web.Lists.Add($line.ListName, “”, $listTemplates[$line.TemplateName])
Write-Host $line.ListName ” list created Successfully…..”
}
$web.Dispose()
$rootweb.Dispose()
$site.Dispose()
}

Read some SharePoint PowerShell tutorials:

Hope this article will be helpful.


Thanks,
Sambita

Check out Best Alternative to InfoPath -> Try Now

You May Also like the Following SharePoint Online Tutorials:

About Sambita Rath

I have 9 years of exprience into SharePoint implementation, architecture, Administrator, development and Training.Designing Information Architecture in SharePoint 2007, 2010,2013 and Office 365.

View all posts by Sambita Rath →