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

How to create document library in SharePoint Online using PowerShell?

One comment

 35,656 total views,  3 views today

Create SharePoint online list using...
Create SharePoint online list using PnP provisioning template

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

Key-Highlights: How to create Document library in SharePoint Online using PowerShell?

  • 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 the document library created by PowerShell is shown on the site
  • How to create a document library in SharePoint Online using PnP PowerShell?
  • Verify that the document library created by PnP PowerShell is shown on 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 placeholder where users can store documents, files, folders, etc.

Few key features in the SharePoint Online document library:

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

When to use the 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 checkout 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.

Create document library in SharePoint Online: How to create 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 the document library created by PowerShell is shown on the site

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

How to create Document library in SharePoint Online using PowerShell
Create a document library in SharePoint Online using the 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 the document library created by PnP PowerShell is shown on the site

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

create document library in SharePoint online using pnp PowerShell
Create a document library in SharePoint Online using PnP PowerShell

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

Thus, in this article we have learned the below with respect to a 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 the document library created by PowerShell is shown on the site
  • How to create a document library in SharePoint Online using PnP PowerShell?
  • Verify that the document library created by PnP PowerShell is shown on the site

See Also: SharePoint Online PowerShell Tutorial

You may also like the following SharePoint Online PowerShell tutorials:

PDF Download Image

Download SharePoint Online & Office 365 Administration eBook

Buy the premium version of SharePoint Online & Office 365 administration eBook from here:



Buy SharePoint Online & Office 365 Administration eBook


 

Get the free demo PDF eBook from here:

FREE DOWNLOAD

Send download link to:

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

 

 

 

About Post Author

1 comments on “How to create document library in SharePoint Online using PowerShell?”

Do you have a better solution or question on this topic? Please leave a comment