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 comes with default metadata properties: "Type", "Name", "Modified" and "Modified By" and can be extended based on our requirements. 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 ="Salaudeen@Crescent.OnMicrosoft.com"
$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!"
and the result:
sharepoint online powershell create document library
Let's add some error handling to the above code

How to Create a Document Library in SharePoint Online using PowerShell?
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
Use this PowerShell to create a list in SharePoint Online: How to Create a List in SharePoint Online using PowerShell?

SharePoint Online: Create 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
SharePoint Online: PowerShell to Create a Document Library SharePoint Online: PowerShell to Create a Document Library Reviewed by Salaudeen Rajack on November 20, 2016 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.