SharePoint Online: Create Multiple Document Libraries using PowerShell

Requirement: Create multiple document libraries in SharePoint Online using PowerShell

PowerShell to Create Multiple Document Libraries in SharePoint Online
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
     
#Function to Create a SharePoint Online document library
Function CreateSPO-DocumentLibrary()
{
    [cmdletbinding()]
    param
    (
        [Parameter(Mandatory=$True,ValueFromPipeline)] [String[]] $LibraryNames,
        [Parameter(Mandatory=$False)] [String] $Description
    )    
    Try {
        #Create Each Document Library
        ForEach($LibraryName in $LibraryNames)
        {
            Write-host -f Yellow "`nEnsuring Document Library '$LibraryName'"
         
            #Get All Existing Lists from the web
            $Lists = $Web.Lists
            $Ctx.Load($Lists)
            $Ctx.ExecuteQuery()
 
            #Check if Library name doesn't exists already
            If(!($Lists.Title -contains $LibraryName))
            {
                #Create Document Library
                $ListInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
                $ListInfo.Title = $LibraryName
                $ListInfo.Description =  $Description
                $ListInfo.TemplateType =  [Microsoft.SharePoint.Client.ListTemplateType]::DocumentLibrary 
                $Web.Lists.Add($ListInfo) | Out-Null
                $Ctx.ExecuteQuery()
  
                write-host  -f Green "`tNew Document Library '$LibraryName' has been created!"
            }
            Else
            {
                Write-Host -f Magenta "`tA Document Library '$LibraryName' Already exist!"
            }
        }
    }
    Catch {
        write-host -f Red "`tError:" $_.Exception.Message
    }
}
 
#Set Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"
 
#Get Credentials to connect
$Cred = Get-Credential
 
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
   
#Get the web from URL
$Web = $Ctx.web
$Ctx.Load($Web)
$Ctx.executeQuery()
 
#Call the function to create document libraries
CreateSPO-DocumentLibrary -LibraryNames @("Document Library 1", "Document Library 2", "Document Library 3", "Document Library 4")

Create Multiple Document Libraries in SharePoint Online using PowerShell
Here is my CSV file with document library details
create multiple document libraries sharepoint online powershell

Let's create document libraries from a CSV file using PowerShell:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    
#Function to Create a SharePoint Online document library
Function CreateSPO-DocumentLibrary()
{
    [cmdletbinding()]
    param
    (
        [Parameter(Mandatory=$True,ValueFromPipeline)] [String[]] $LibraryNames,
        [Parameter(Mandatory=$False)] [String] $Description,
        [Parameter(Mandatory=$False)] [Switch] $ShowOnQuickLaunch
    )    
    Try {
        #Create Each Document Library
        ForEach($LibraryName in $LibraryNames)
        {
            Write-host -f Yellow "`nEnsuring Document Library '$LibraryName'"
        
            #Get All Existing Lists from the web
            $Lists = $Web.Lists
            $Ctx.Load($Lists)
            $Ctx.ExecuteQuery()

            #Check if Library name doesn't exists already
            If(!($Lists.Title -contains $LibraryName))
            {
                #Set Quick Launch Option
                If($ShowOnQuickLaunch.IsPresent)
                {
                    $QuickLaunchOption = [Microsoft.SharePoint.Client.QuickLaunchOptions]::On
                }
                Else
                {
                    $QuickLaunchOption = [Microsoft.SharePoint.Client.QuickLaunchOptions]::Off
                }

                #Create Document Library
                $ListInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
                $ListInfo.Title = $LibraryName
                $ListInfo.Description =  $Description
                $ListInfo.TemplateType =  [Microsoft.SharePoint.Client.ListTemplateType]::DocumentLibrary 
                $List = $Web.Lists.Add($ListInfo)
                $List.OnQuickLaunch = $QuickLaunchOption
                $List.Update()
                $Ctx.ExecuteQuery()
 
                write-host  -f Green "`tNew Document Library '$LibraryName' has been created!"
            }
            Else
            {
                Write-Host -f Magenta "`tA Document Library '$LibraryName' Already exist!"
            }
        }
    }
    Catch {
        write-host -f Red "`tError:" $_.Exception.Message
    }
}

#Set Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"
$CSVFilePath = "C:\Temp\DocLibs.csv"

#Get Credentials to connect
$Cred = Get-Credential

#Get the CSV file
$CSVFile = Import-Csv $CSVFilePath 

#Read CSV file and create document document library
ForEach($Line in $CSVFile)
{
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Get the web from URL
    $Web = $Ctx.web
    $Ctx.Load($Web)
    $Ctx.executeQuery()
    
    #Get the ShowOnQuickLaunch option
    $ShowOnQuickLaunch = [System.Convert]::ToBoolean($Line.ShowOnQuickLaunch)

    #Call the function to create document libraries
    If($ShowOnQuickLaunch -eq $True)
    {
        CreateSPO-DocumentLibrary -LibraryNames $Line.LibraryName -Description $Line.Description -ShowOnQuickLaunch
    }
    Else
    {
        CreateSPO-DocumentLibrary -LibraryNames $Line.LibraryName -Description $Line.Description
    }
}
SharePoint Online: Create Multiple Document Libraries using PowerShell SharePoint Online: Create Multiple Document Libraries using PowerShell Reviewed by Salaudeen Rajack on August 06, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.