Data / Provision

Bulk library generation

With this sample, you can create a whole bunch of libraries at once.

Summary

With this sample, you can create a whole bunch of libraries at once. These are fairly simple libraries with an extra view and a few extra columns.

Example Screenshot

Script parameters

Parameter Mandatory Description Default value
TargetWebUrl Yes Target SharePoint site
AmountLibraries No Amount of libraries to create 5
LibraryName No Name for your libraries Parker's Library
LibraryTemplate No Library template to use for bulk creation DocumentLibrary
LibraryFields No Fields to add to your library Description,Status
LibraryView No View name to add to your library Parker's Perspective
powershell

# Usage example: 
#     .\Create-Bulk-Libraries.ps1 -TargetSitePartUrl mytargetsite -PartTenant contoso -AmountLibraries 3

[CmdletBinding()]
param (
    [Parameter(Mandatory = $true, HelpMessage = "Target site e.g. https://contoso.sharepoint.com/sites/Intranet")]
    [string]$TargetWebUrl,

    [Parameter(Mandatory = $true, HelpMessage = "Organisation url fragment e.g. contoso ")]
    [string]$PartTenant,

    [Parameter(Mandatory = $false, HelpMessage = "Amount of libraries to create e.g. 5 ")]
    [float]$AmountLibraries = 5,

    [Parameter(Mandatory = $false, HelpMessage = "Name for your libraries e.g. Parker's Library ")]
    [string]$LibraryName = "Parker's Library",

    [Parameter(Mandatory = $false, HelpMessage = "Library template to use for bulk creation e.g. DocumentLibrary ")]
    [string]$LibraryTemplate = "DocumentLibrary",

    [Parameter(Mandatory = $false, HelpMessage = "Fields to add to your library e.g. Description,Status ")]
    [string[]]$LibraryFields = @("Description", "Status"),

    [Parameter(Mandatory = $false, HelpMessage = "View name to add to your library e.g. Parker's Perspective ")]
    [string]$LibraryView = "Parker's Perspective"
)
begin {
    Write-Host "`nConnecting to " $TargetWebUrl
        
    Connect-PnPOnline -Url $TargetWebUrl -ReturnConnection -Interactive | Out-Null
}
process {
    Write-Host "`nCreating new libraries..." -ForegroundColor Cyan
        
    for ($counter = 1; $counter -le $AmountLibraries; $counter++ )
    {
      $newList = New-PnPList -Title "$($LibraryName) $($counter)" -Template $LibraryTemplate

      if ($null -ne $LibraryFields) {  
        foreach ($fieldName in $LibraryFields) {
          Add-PnPField -list $newList -InternalName $fieldName.replace(' ','') -DisplayName $fieldName -Type "Text" | Out-Null
        }
      }

      if ($null -ne $LibraryView) {  
        Add-PnPView  -list $newList -Title $LibraryView -Fields (,"Name" + $LibraryFields) | Out-Null
      }

      Write-Progress -Activity "Library creation" -Status "$counter libraries created" -PercentComplete (($counter / $AmountLibraries) * 100)
    }

    Write-Host "`nScript Complete! :)" -ForegroundColor Green
}

Contributors

Author(s)
Jasey Waegebaert
Milan Holemans

Disclaimer

THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

Tool documentation & references

Keep exploring