How to upload a page to a Document libarary using SharePoint online Powershell?

Hello SharePointers,

In this blog, you will find a piece of code to upload a file to a document library using SharePoint online Powershell.
$FolderName = “C:\Myfolders”
$DocLibName = “CustomLibrary”
Add-Type -Path “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll”
Add-Type -Path “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll”
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Credent = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Credent
$List = $Context.Web.Lists.GetByTitle($DocLibName)
$Context.Load($List)
$Context.ExecuteQuery()
Foreach ($File in (dir $FolderName -File))
{
$FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $FileStream
$FileCreationInfo.URL = $File
$Upload = $List.RootFolder.Files.Add($FileCreationInfo)
$Context.Load($Upload)
$Context.ExecuteQuery()
}

Happy SharePointing Folks!!

Leave a Reply

Your email address will not be published.