kwizcom banner advertisement

SharePoint Online: Upload Files to Document Library using PowerShell

Requirement: Upload files to SharePoint online using PowerShell.
sharepoint online Upload Files to Document Library using PowerShell

How to Upload a File to SharePoint Online Library using PowerShell:
Lets upload a document using PowerShell in SharePoint Online. Here is how to upload files to sharepoint online.

#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
$WebUrl = "https://crescent.sharepoint.com/Sites/Sales/"
$LibraryName ="Documents"
$SourceFile="C:\SitesToCreate.csv"
$AdminName ="Salaudeen@crescent.com"
$AdminPassword ="password goes here"
 
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($AdminName,(ConvertTo-SecureString $AdminPassword -AsPlainText -Force))
 
#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($WebUrl) 
$Context.Credentials = $Credentials

#Get the Library
$Library =  $Context.Web.Lists.GetByTitle($LibraryName)

#Get the file from disk
$FileStream = ([System.IO.FileInfo] (Get-Item $SourceFile)).OpenRead()
#Get File Name from source file path
$SourceFileName = Split-path $SourceFile -leaf
  
#Upload the File to SharePoint Library
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $FileStream
$FileCreationInfo.URL = $SourceFileName
$FileUploaded = $Library.RootFolder.Files.Add($FileCreationInfo)
 
#Execute
$Context.Load($FileUploaded) 
$Context.ExecuteQuery() 

#Close file stream
$FileStream.Close()
 
write-host "File has been uploaded!" 

Upload All Files from a Folder to SharePoint online Library:
Now, lets upload all files from a local folder to SharePoint online library 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
$WebUrl = "https://crescent.sharepoint.com/Sites/Sales/"
$LibraryName ="Documents"
$SourceFolder="C:\Documents"
$UserName ="Salaudeen@crescent.com"
$Password ="password goes here"
 
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
 
#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($WebUrl) 
$Context.Credentials = $Credentials

#Get the Library
$Library =  $Context.Web.Lists.GetByTitle($LibraryName)

#upload each file from the directory
Foreach ($File in  (dir $SourceFolder -File))
{
    #Get the file from disk
    $FileStream = ([System.IO.FileInfo] (Get-Item $File.FullName)).OpenRead()
  
    #Upload the File to SharePoint Library
    $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
    $FileCreationInfo.Overwrite = $true
    $FileCreationInfo.ContentStream = $FileStream
    $FileCreationInfo.URL = $File
    $FileUploaded = $Library.RootFolder.Files.Add($FileCreationInfo)
 
    #Execute
    $Context.Load($FileUploaded) 
    $Context.ExecuteQuery() 

    #Close file stream
    $FileStream.Close()

    write-host "File: $($File) has been uploaded!"
} 
This uploads files to sharepoint online using PowerShell, and here is the result:
sharepoint online upload document powershell
Tags: sharepoint online upload file powershell, powershell upload document to sharepoint online, sharepoint online powershell upload file, powershell script to upload files to sharepoint online
SharePoint Online: Upload Files to Document Library using PowerShell SharePoint Online: Upload Files to Document Library using PowerShell Reviewed by Salaudeen Rajack on June 04, 2016 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.