SharePoint Online: Upload Folder Structure using PowerShell

Requirement: Upload folder structure to SharePoint Online using PowerShell

PowerShell to Upload Folder Structure from to SharePoint Online:
Here is the PowerShell to upload a folder structure from local drive to SharePoint Online document library.
#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 Yellow "`tFolder Already Exists! Skipped...!"
    }
    Catch {
        #Create New Sub-Folder
        $Folder=$Web.Folders.Add($FolderRelativeURL)
        $Ctx.ExecuteQuery()
        Write-host -f Green "`tCreated Folder at "$FolderRelativeURL
    }
}
  
#Function to upload a Local Folder structure to SharePoint Online Documnet Library
Function Upload-SPOFolderStructure()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $SourceFolderPath
    )

    #Setup Credentials to connect
    $Cred= Get-Credential
  
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
       
    #Get the Target Folder to Upload
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $List = $Web.Lists.GetByTitle($ListName)
    $Ctx.Load($List)
    $TargetFolder = $List.RootFolder
    $Ctx.Load($TargetFolder)
    $Ctx.ExecuteQuery()
 
    #Get All Folders and Sub-Folders from the Local 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..."
                #Call function to ensure folder
                Ensure-SPOFolder -FolderRelativeURL $FolderRelativeURL
            }
        }
    }
}
 
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$ListName="Documents"
$SourceFolderPath="C:\Users\salaudeen\Desktop\Upload"
  
#Call the function to Upload All folders and sub-folders from local folder to SharePoint Online library
Upload-SPOFolderStructure -SiteURL $SiteURL -ListName $ListName -SourceFolderPath $SourceFolderPath

This script copies all folders and sub-folders from the given source to SharePoint Online document library.
SharePoint Online Upload Folder Structure using PowerShell

If you need to upload folder structure along with files, use: Upload Folder to SharePoint Online using PowerShell
SharePoint Online: Upload Folder Structure using PowerShell SharePoint Online: Upload Folder Structure using PowerShell Reviewed by Salaudeen Rajack on July 14, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.