Office 365: How to create document library in SharePoint Online using PowerShell?

No comments

 564 total views,  8 views today

In this tutorial, we will learn about how to create document library in SharePoint Online using PowerShell script and PnP PowerShell.

Key-Highlights:

  • What is the document library in SharePoint?
  • When to use the document library in SharePoint Online?
  • How to create a document library in SharePoint Online using PowerShell script?
  • Verify that document library is created by PowerShell is shown in the site
  • How to create a document library in SharePoint Online using PnP PowerShell?
  • Verify that document library is created by PnP PowerShell is shown in the site

What is the document library in SharePoint Online?

As we know SharePoint is one of the best content management systems, and when we talk about storing the document -it is the document library, we can say it is a place holder where user can store documents, files, folders etc.

Few key feautres in SharePoint Online document libray:

  • A secure place to store files, folder, documents etc.
  • Documents are easily findable by users and co-workers.
  • Documents stored in document libary are highly secured.
  • With a single document multiple users can work together this feature is called co-authoring.
  • Documents are versionable – user can maintain multiple versions (major & minor).

When to use document library in SharePoint Online?

Use a document library to store, organize, sync, and share documents with people. You can use co-authoring, versioning, and check out to work on documents together. With your documents in one place, everybody can get the latest versions whenever they need them. You can also sync your documents to your local computer for offline access.

For example, you can use a document library on a site in SharePoint to store all files related to a specific project or a specific client. Adding files or moving files between folders is as easy as dragging and dropping them from one location to another.

How to create a document library in SharePoint Online using PowerShell script?


#The below script will create a document library in SharePoint online using PowerShell and PnP PowerShell which will take site URL, document library name, UserName and Password as paramaters.
#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"
cls

$fileName = "Create_Document_Library_Report"
#'yyyyMMddhhmm yyyyMMdd
$enddate = (Get-Date).tostring("yyyyMMddhhmmss")
#$filename = $enddate + '_VMReport.doc'
$logFileName = $fileName +"_"+ $enddate+"_Log.txt"
$invocation = (Get-Variable MyInvocation).Value
$directoryPath = Split-Path $invocation.MyCommand.Path

$directoryPathForLog=$directoryPath+"\"+"LogFiles"
if(!(Test-Path -path $directoryPathForLog))
{
New-Item -ItemType directory -Path $directoryPathForLog
#Write-Host "Please Provide Proper Log Path" -ForegroundColor Red
}
#$logPath = $directoryPath + "\" + $logFileName

$logPath = $directoryPathForLog + "\" + $logFileName

$isLogFileCreated = $False

#DLL Location

$directoryPathForDLL=$directoryPath+"\"+"Dependency Files"
if(!(Test-Path -path $directoryPathForDLL))
{
New-Item -ItemType directory -Path $directoryPathForDLL
#Write-Host "Please Provide Proper Log Path" -ForegroundColor Red
}

#DLL location
$clientDLL=$directoryPathForDLL+"\"+"Microsoft.SharePoint.Client.dll"
$clientDLLRuntime=$directoryPathForDLL+"\"+"Microsoft.SharePoint.Client.dll"

Add-Type -Path $clientDLL
Add-Type -Path $clientDLLRuntime
#File Download location

$directoryPathForFileDownloadLocation=$directoryPath+"\"+"Downloaded Files"
if(!(Test-Path -path $directoryPathForFileDownloadLocation))
{
New-Item -ItemType directory -Path $directoryPathForFileDownloadLocation
#Write-Host "Please Provide Proper Log Path" -ForegroundColor Red
}

#File Download location

function Write-Log([string]$logMsg)
{
if(!$isLogFileCreated){
Write-Host "Creating Log File..."
if(!(Test-Path -path $directoryPath))
{
Write-Host "Please Provide Proper Log Path" -ForegroundColor Red
}
else
{
$script:isLogFileCreated = $True
Write-Host "Log File ($logFileName) Created..."
[string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg)
Add-Content -Path $logPath -Value $logMessage
}
}
else
{
[string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg)
Add-Content -Path $logPath -Value $logMessage
}
}

#The below function will create a document library in SharePoint Online which will take siteURL, DocumentLibraryName,DocumentLibraryDescription, UserName and Password as paramaters.
Function CreateDocumentLibraryInSPO()
{
param
(
[Parameter(Mandatory=$true)] [string] $SPOSiteURL,
[Parameter(Mandatory=$true)] [string] $DocumentLibraryName,
[Parameter(Mandatory=$false)][string] $DocumentLibraryDescription,
[Parameter(Mandatory=$true)] [string] $UserName,
[Parameter(Mandatory=$true)] [string] $Password
)

Try
{

$securePassword= $Password | ConvertTo-SecureString -AsPlainText -Force
#Setup the Context
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SPOSiteURL)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $securePassword)
#Get All Lists Libraries from the web object (site).
$allLists = $ctx.Web.Lists
$ctx.Load($allLists)
$ctx.ExecuteQuery()

#Before creating a document libary, check whether document library doesn't exists, if not create a new one.
if(!($allLists.Title -contains $DocumentLibraryName))
{
#create document library in sharepoint online powershell
$ListCreationInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$ListCreationInfo.Title = $DocumentLibraryName
$ListCreationInfo.TemplateType = 101 #Document Library
$ListToAdd = $ctx.Web.Lists.Add($ListCreationInfo)
$ListToAdd.Update()
$ctx.ExecuteQuery()

Write-host -f Green "The new document library '$DocumentLibraryName' has been created successfully!"
}
else
{
Write-Host -f Yellow "The library '$DocumentLibraryName' already exist in the site!"
}

}
Catch
{

$ErrorMessage = $_.Exception.Message +"in creating document library in SPO!:"
Write-Host $ErrorMessage -BackgroundColor Red
Write-Log $ErrorMessage

}

}
#############Testing the CreateDocumentLibraryInSPO function ############################################################

#Parameters value
$siteURL="https://globalsharepoint2020.sharepoint.com/sites/CustomSearchRND"
$documentLibrarNameToCreate="Document Library created by PowerShell"
$documentLibrarNameToCreateDescription="This is a test document library"
$userName = "Global-sharepoint2020@globalsharepoint2020.onmicrosoft.com"
$passWord = "YourSPOPassword"
#Parameters ends here.
#Calling the CreateDocumentLibraryInSPO function and passing the parameters.

CreateDocumentLibraryInSPO $siteURL $documentLibrarNameToCreate $documentLibrarNameToCreateDescription $userName $passWord

#############Testing the CreateDocumentLibraryInSPO function ends here ###################################################

Verify that document library is created by PowerShell is shown in the site

Once we execute the above PowerShell script, we can see  that the “Document Library created by PowerShell” is created in the site.

Create document library in SharePoint Online using PowerShell script
Create document library in SharePoint Online using PowerShell script

How to create document library in SharePoint Online using PnP PowerShell?


#############Creating a document library in SharePoint online using PnP PowerShell #######################################

CLS
$siteURL="https://globalsharepoint2020.sharepoint.com/sites/CustomSearchRND/"
$documentLibrarNameToCreateUsingPnP="PNP Document Library"
$userName = "Global-sharepoint2020@globalsharepoint2020.onmicrosoft.com"
$passWord = "YourSPOPassword"
$encPassWord = convertto-securestring -String $passWord -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $userName, $encPassWord

Connect-PnPOnline -Url $siteURL -Credentials $cred

#sharepoint online create document library powershell
New-PnPList -Title $documentLibrarNameToCreateUsingPnP -Template DocumentLibrary -OnQuickLaunch

#############Creating a document library in SharePoint online using PnP PowerShell ends here ###########################

Verify that document library is created by PnP PowerShell is shown in the site

Once we execute the above PowerShell script, we can see that the “PNP Document Library” is created in the site.

Create document library in SharePoint Online using PnP PowerShell
Create document library in SharePoint Online using PnP PowerShell

Summary: What we had here?

Thus, in this article we have learned the below with respect to document library in SharePoint online:

  • What is the document library in SharePoint?
  • When to use the document library in SharePoint Online?
  • How to create a document library in SharePoint Online using PowerShell script?
  • Verify that document library is created by PowerShell is shown in the site
  • How to create document library in SharePoint Online using PnP PowerShell?
  • Verify that document library is created by PnP PowerShell is shown in the site

See Also

You may also like the following SharePoint PowerShell tutorials:

Free download SharePoint Online & Office 365 Administration eBook(238 Pages)

Get the PDF eBook from here:


[purchase_link id="0" style="button" color="blue" text="Free Download SharePoint Online & Office 365 Administration"]

 

 
FREE DOWNLOAD

Send download link to:

Subscribe to get exclusive content and recommendations every month. You can unsubscribe anytime.