PowerShell Script to create Bulk Lists and Libraries in SharePoint
1) In this post we will look on how to create bulk or multiple lists/libraries and associate multiple content types to them in SharePoint.
2) Follow the below posts where I have covered
Bulk Creation of Site Columns: http://www.sharepointbasic.com/2016/11/powershell-script-to-create-bulk-site_27.html
Bulk Creation of Site Content Types: http://www.sharepointbasic.com/2016/12/powershell-script-to-create-bulk-site.html
3) Create a CSV in the below format.
4)
Create a powershell script as shown below
#This script creates site columns from the given csv file #Author: Prem Kumar #Date: Feb 11 2015 param( [string]$webUrl = $(Read-Host -prompt "Root Web Application Url...?"), [string]$CsvFilePath = $(Read-Host -prompt "CSV Path...?") ) try { function CreateSharePointLibrary { [CmdletBinding()] Param( [Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)] [string]$webUrl, [Parameter(Mandatory=$true, Position=1)] [string]$LibraryName, [Parameter(Mandatory=$true, Position=2)] [string]$Description, [Parameter(Mandatory=$false, Position=3)] [string]$LibraryTemplate ) Process { Start-SPAssignment -Global $spWeb = Get-SPWeb -Identity $webUrl $spListCollection = $spWeb.Lists $spLibrary = $spListCollection.TryGetList($LibraryName) if($spLibrary -ne $null) { write-host "Library $LibraryName already exists in the site" } else { write-host "Creating Library - $LibraryName" $spListCollection.Add($LibraryName, $Description, $LibraryTemplate) write-host "Added List! $LibraryName" } $spWeb.Dispose() Stop-SPAssignment -Global } } function AddContentTypes{ [CmdletBinding()] Param( [Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)] [string]$webUrl, [Parameter(Mandatory=$true, Position=1)] [string]$LibraryName, [Parameter(Mandatory=$true, Position=2)] [string]$CTtoAdd ) Process { Start-SPAssignment -Global $rootweb= Get-SPWeb $webUrl write-host "Checking site: $rootweb.Title" #Make sure content types are allowed on the list specified $docLibraryColln = $rootweb.Lists $docLibrary = $docLibraryColln.TryGetList($LibraryName) if ($docLibrary -ne $null) { $docLibrary.ContentTypesEnabled = $true $docLibrary.Update() #Add site content types to the list $customCT = $rootweb.ContentTypes[$CTtoAdd] $docLibrary.ContentTypes.Add($customCT) write-host "Content type added to list $docLibrary.Title" $docLibrary.Update() } else { write-host "The list $lookForList does not exist in site $site.Title" } #Dispose of the site object $rootweb.Dispose() Stop-SPAssignment -Global } } $listsData= Import-Csv $CsvFilePath foreach($item in $listsData) { write-host "Adding List : $item" $Template = $item.ListTemplate $LibraryTemplateDL = [Microsoft.SharePoint.SPListTemplateType]::$Template #Function Calling CreateSharePointLibrary $webUrl $item.ListName $item.Description $LibraryTemplateDL if($item.ContentType -ne "None") { AddContentTypes $webUrl $item.ListName $item.ContentType } } } catch { write-host "Exception in Creating List $_.Exception.Message" } finally { write-host "Exit of List ..." }
Comments
Post a Comment