none
How to upload folder structure with all files to SharePoint Library using CSOM in Powershell RRS feed

  • Question

  • Hi Team,

    I want to upload all documents with folder structure(sub folders and files) to SharePoint Library using CSOM powershell script.

    Please provide CSOM powershell script to resolve this functionality.

    Thanks,

    Raghava.

    Monday, December 2, 2019 10:17 AM

Answers

  • Hi Raghava,

    Here is the CSOM PowerShell script to upload all documents includes sub folder struct to SharePoint Online library for your reference:

    #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 to Check if Folder Exists. If not, Create the Folder
    Function Ensure-SPOFolder()
    {
        param
        (
            [Parameter(Mandatory=$true)] [string] $FolderRelativeURL
        )
        #Check Folder Exists
        Try { 
            $Folder = $Web.GetFolderByServerRelativeUrl($FolderRelativeURL)
            $Ctx.Load($Folder)
            $Ctx.ExecuteQuery() 
            #Write-host -f Green "Folder Already Exists!"
        }
        Catch {
            #Create New Sub-Folder
            $Folder=$Web.Folders.Add($FolderRelativeURL)
            $Ctx.ExecuteQuery()
            Write-host -f Green "Created Folder at "$FolderRelativeURL
        }
    }
    
    #Function to Upload a File to a SharePoint Online
    Function Upload-SPOFile()
    {
        param
        (
            [Parameter(Mandatory=$true)] [string] $SourceFilePath,
            [Parameter(Mandatory=$true)] [string] $TargetFileURL
        )    
        #Get the file from disk
        $FileStream = ([System.IO.FileInfo] (Get-Item $SourceFilePath)).OpenRead()
    	
        #Get File Name from source file path
        $SourceFileName = Split-path $SourceFilePath -leaf
        
        #Upload the File to SharePoint Library
        $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
        $FileCreationInfo.Overwrite = $true
        $FileCreationInfo.ContentStream = $FileStream
        $FileCreationInfo.URL = $TargetFileURL
        $FileUploaded = $TargetFolder.Files.Add($FileCreationInfo)
        $Ctx.ExecuteQuery()  
        #Close file stream
        $FileStream.Close()
        Write-host "File '$TargetFileURL' Uploaded Successfully!" -ForegroundColor Green
    }
    #Main Function to upload a Local Folder to SharePoint Online Documnet Library Folder
    Function Upload-SPOFolder()
    { 
        param
        (
            [Parameter(Mandatory=$true)] [string] $SourceFolderPath,
            [Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $TargetFolder       
        )
    
        #Get All Files and Sub-Folders from Source
        Get-ChildItem $SourceFolderPath -Recurse | ForEach-Object {
            If ($_.PSIsContainer -eq $True) 
            {
                $FolderRelativeURL = $TargetFolder.ServerRelativeURL+$_.FullName.Replace($SourceFolderPath,"").Replace("\","/")
                If($FolderRelativeURL) 
                {
                    Write-host -f Yellow "Ensuring Folder '$FolderRelativeURL' Exists..."
                    Ensure-SPOFolder -FolderRelativeURL $FolderRelativeURL
                }  
            }
            Else
            {
                $FolderRelativeUrl = $TargetFolder.ServerRelativeURL + $_.DirectoryName.Replace($SourceFolderPath,"").Replace("\","/")
                $FileRelativeURL = $FolderRelativeUrl+"/"+$_.Name
                Write-host -f Yellow "Uploading File '$_' to URL "$FileRelativeURL
                Upload-SPOFile -SourceFilePath $_.FullName -TargetFileURL $FileRelativeURL
            }
        }
    }
    
    #Set parameter values
    $SiteURL="https://zheguo.sharepoint.com/sites/dev"
    $LibraryName="TestDoc"
    $SourceFolderPath="C:\Documents"
    
    #Setup Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
    
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
    
    #Get the Target Folder to Upload
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $List = $Web.Lists.GetByTitle($LibraryName)
    $Ctx.Load($List)
    $Ctx.Load($List.RootFolder)
    $Ctx.ExecuteQuery() 
    
    #Call the function to Upload All files & folders from local folder to SharePoint Online
    Upload-SPOFolder -SourceFolderPath $SourceFolderPath -TargetFolder $List.RootFolder

    Reference:

    SharePoint Online: Upload a Folder using PowerShell

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Tuesday, December 3, 2019 2:18 AM

All replies

  • FYI

    https://gallery.technet.microsoft.com/PowerShell-Bulk-Upload-b9e9d600


    Justin Liu Office Apps & Services MVP, MCSE
    Senior Software Engineer
    Learn Microsoft 365 from Microsoft DOCs now!
    Please Vote and Mark as Answer if it helps you.

    Tuesday, December 3, 2019 2:17 AM
  • Hi Raghava,

    Here is the CSOM PowerShell script to upload all documents includes sub folder struct to SharePoint Online library for your reference:

    #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 to Check if Folder Exists. If not, Create the Folder
    Function Ensure-SPOFolder()
    {
        param
        (
            [Parameter(Mandatory=$true)] [string] $FolderRelativeURL
        )
        #Check Folder Exists
        Try { 
            $Folder = $Web.GetFolderByServerRelativeUrl($FolderRelativeURL)
            $Ctx.Load($Folder)
            $Ctx.ExecuteQuery() 
            #Write-host -f Green "Folder Already Exists!"
        }
        Catch {
            #Create New Sub-Folder
            $Folder=$Web.Folders.Add($FolderRelativeURL)
            $Ctx.ExecuteQuery()
            Write-host -f Green "Created Folder at "$FolderRelativeURL
        }
    }
    
    #Function to Upload a File to a SharePoint Online
    Function Upload-SPOFile()
    {
        param
        (
            [Parameter(Mandatory=$true)] [string] $SourceFilePath,
            [Parameter(Mandatory=$true)] [string] $TargetFileURL
        )    
        #Get the file from disk
        $FileStream = ([System.IO.FileInfo] (Get-Item $SourceFilePath)).OpenRead()
    	
        #Get File Name from source file path
        $SourceFileName = Split-path $SourceFilePath -leaf
        
        #Upload the File to SharePoint Library
        $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
        $FileCreationInfo.Overwrite = $true
        $FileCreationInfo.ContentStream = $FileStream
        $FileCreationInfo.URL = $TargetFileURL
        $FileUploaded = $TargetFolder.Files.Add($FileCreationInfo)
        $Ctx.ExecuteQuery()  
        #Close file stream
        $FileStream.Close()
        Write-host "File '$TargetFileURL' Uploaded Successfully!" -ForegroundColor Green
    }
    #Main Function to upload a Local Folder to SharePoint Online Documnet Library Folder
    Function Upload-SPOFolder()
    { 
        param
        (
            [Parameter(Mandatory=$true)] [string] $SourceFolderPath,
            [Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $TargetFolder       
        )
    
        #Get All Files and Sub-Folders from Source
        Get-ChildItem $SourceFolderPath -Recurse | ForEach-Object {
            If ($_.PSIsContainer -eq $True) 
            {
                $FolderRelativeURL = $TargetFolder.ServerRelativeURL+$_.FullName.Replace($SourceFolderPath,"").Replace("\","/")
                If($FolderRelativeURL) 
                {
                    Write-host -f Yellow "Ensuring Folder '$FolderRelativeURL' Exists..."
                    Ensure-SPOFolder -FolderRelativeURL $FolderRelativeURL
                }  
            }
            Else
            {
                $FolderRelativeUrl = $TargetFolder.ServerRelativeURL + $_.DirectoryName.Replace($SourceFolderPath,"").Replace("\","/")
                $FileRelativeURL = $FolderRelativeUrl+"/"+$_.Name
                Write-host -f Yellow "Uploading File '$_' to URL "$FileRelativeURL
                Upload-SPOFile -SourceFilePath $_.FullName -TargetFileURL $FileRelativeURL
            }
        }
    }
    
    #Set parameter values
    $SiteURL="https://zheguo.sharepoint.com/sites/dev"
    $LibraryName="TestDoc"
    $SourceFolderPath="C:\Documents"
    
    #Setup Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
    
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
    
    #Get the Target Folder to Upload
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $List = $Web.Lists.GetByTitle($LibraryName)
    $Ctx.Load($List)
    $Ctx.Load($List.RootFolder)
    $Ctx.ExecuteQuery() 
    
    #Call the function to Upload All files & folders from local folder to SharePoint Online
    Upload-SPOFolder -SourceFolderPath $SourceFolderPath -TargetFolder $List.RootFolder

    Reference:

    SharePoint Online: Upload a Folder using PowerShell

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Tuesday, December 3, 2019 2:18 AM
  • Hi Jerry,

    Thank you ans its working good.

    Thanks,

    Raghava.

    Tuesday, December 3, 2019 7:58 AM