SharePoint Online: PowerShell to Create a Document Library

Requirement: Create a Document Library in SharePoint Online using PowerShell.

Create Document Library using PowerShell in SharePoint Online:
The document library is the most commonly used type of library in SharePoint Online used to store and collaborate Office documents such as Word, Excel, PowerPoint, etc. SharePoint Online document libraries come with default metadata properties: "Type", "Name", "Modified" and "Modified By" and can be extended based on our requirements. Make sure you have the "Edit" permission level to the site where you would like to create a document library, before proceeding to the following steps! Let's see how to create a new document library in SharePoint Online.

How to Create a document library in SharePoint Online?
To create a new document library in the SharePoint Online site, follow these steps:
  • Navigate to the SharePoint Online site in the browser
  • Click on Settings gear >> Choose "Site Contents".
  • In the Site Contents page, click on the "New" menu and then choose "Document library" option.
    powershell script to create document library sharepoint online
  • Provide a name and description for the library. Set "Show in site navigation" Checkbox as per your requirement and then click on the "Create" button to create the new document library.
    create document library in sharepoint online using powershell
This will create a new document library and your browser should take you there! Let's see how to create libraries in SharePoint Online with PowerShell.

SharePoint Online: PowerShell to Create New Document Library
Alright, How to create a document library in SharePoint Online 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"

#Variables for Processing
$SiteURL = "https://Crescent.sharepoint.com/Sites/Sales"
$LoginName ="[email protected]"
$LoginPassword ="Password" 

#Get the Client Context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)

#supply Login Credentials
$SecurePWD = ConvertTo-SecureString $LoginPassword –asplaintext –force  
$Credential = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($LoginName,$SecurePWD)
$Context.Credentials = $Credential

#powershell create document library sharepoint online
$ListInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$ListInfo.Title = "Project Docs"
$ListInfo.TemplateType = 101 #Document Library
$List = $Context.Web.Lists.Add($ListInfo)
$List.Description = "Repository to store project artifacts"
$List.Update()
$Context.ExecuteQuery()

write-host "New Document Library has been created!"
Let's add some error handling to the above script and create a document library SharePoint Online with PowerShell.

How to Create a Document Library in SharePoint Online using PowerShell?
Document Library is a container to store any type of file in the SharePoint Online site. Here is the PowerShell for SharePoint Online to create a document library with some error handling added.
#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 Create-DocumentLibrary()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $DocLibraryName
    )    
    Try {
    #Setup Credentials to connect
    $Cred = Get-Credential
    $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

    #Get All Lists from the web
    $Lists = $Ctx.Web.Lists
    $Ctx.Load($Lists)
    $Ctx.ExecuteQuery()

    #Check if Library name doesn't exists already and create document library
    if(!($Lists.Title -contains $DocLibraryName))
    { 
        #create document library in sharepoint online powershell
        $ListInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
        $ListInfo.Title = $DocLibraryName
        $ListInfo.TemplateType = 101 #Document Library
        $List = $Ctx.Web.Lists.Add($ListInfo)
        $List.Update()
        $Ctx.ExecuteQuery()
 
        write-host  -f Green "New Document Library has been created!"
    }
    else
    {
        Write-Host -f Yellow "List or Library '$DocLibraryName' already exist!" 
    }
}
Catch {
    write-host -f Red "Error Creating Document Library!" $_.Exception.Message
}
}

#Set Parameters
$SiteURL= "https://crescent.sharepoint.com"
$DocLibraryName="Project Docs"

#Call the function to create document library
Create-DocumentLibrary -siteURL $SiteURL -DocLibraryName $DocLibraryName
You can also specify the "URL" property to set the URL of a document library. E.g. You may want to remove the %20 characters from the URL!

Use this PowerShell to create a list in SharePoint Online: How to Create a List in SharePoint Online using PowerShell?

SharePoint Online: Add a Document Library using PnP PowerShell
Here is how to create a document library in SharePoint Online using PnP PowerShell:
#Set Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$LibraryName = "Project Documents"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#sharepoint online create document library powershell
New-PnPList -Title $LibraryName -Template DocumentLibrary -OnQuickLaunch
If you want to create multiple document libraries in SharePoint Online from a CSV file, use: Create Multiple Document Libraries in SharePoint Online using PowerShell

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.